2.1 Keywords and Identifiers
Keywords are reserved words with fixed meaning to the compiler (int, return, if, for, struct, etc.) — there are 32 in standard C — and cannot be used as variable names. Identifiers are the names a programmer chooses for variables, functions, arrays, etc. They must start with a letter or underscore, contain only letters/digits/underscores, and are case-sensitive.
The standard 32 C keywords can be grouped by functionality:
- Data Types (8):
char,int,float,double,short,long,signed,unsigned. - Control Flow (12):
if,else,switch,case,default,for,while,do,break,continue,goto,return. - Storage Classes & Modifiers (6):
auto,register,static,extern,const,volatile. - User-Defined Types & Tools (6):
struct,union,enum,typedef,sizeof,void.
Identifier Naming Rules:
- Valid identifier characters: uppercase letters (A–Z), lowercase letters (a–z), digits (0–9), and underscore (
_). - The first character must be an alphabet letter or an underscore. It cannot be a digit (e.g.
2scoreis invalid;score2is valid). - C is strictly case-sensitive:
total,Total, andTOTALrepresent three completely separate identifiers.
2.2 Data Types
C classifies data types into Primary (built-in: int, char, float, double, void), Derived (built from primary types: arrays, pointers, functions), and User-defined (struct, union, enum, typedef). Each type reserves a fixed number of bytes, which determines the range of values it can store.
| Type | Typical Size | Example Range |
|---|---|---|
char | 1 byte | -128 to 127 |
int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
float | 4 bytes | ~6-7 significant digits |
double | 8 bytes | ~15-16 significant digits |
void | 0 bytes | represents 'no value' |
2.3 Variables and Constants
A variable is a named, typed storage location whose value can change during execution; it must be declared before use (int age;) and can be initialized at declaration (int age = 20;). A constant is a value that cannot change — created with the const keyword (const float PI = 3.14;) or with #define PI 3.14, which the preprocessor textually substitutes before compilation.
Variables: Declaration vs Definition vs Initialization:
- Declaration (
extern int score;): Informs the compiler of a variable's name and type without allocating memory at that instant. - Definition (
int score;): Causes the compiler to allocate physical storage space in memory (e.g. 4 bytes on stack or BSS segment). - Initialization (
int score = 100;): Combines storage allocation with assigning an immediate initial value.
Constants: const vs #define:
const float PI = 3.14f;declares a typed, read-only variable allocated in memory and strictly verified by compiler type-checking.#define PI 3.14is a textual substitution macro performed during the preprocessing phase; it consumes no variable memory and has no type constraints.
2.4 Operators and Expressions
Operators act on operands to form expressions. C provides Arithmetic (+ - * / %), Relational (== != > < >= <=), Logical (&& || !), Assignment (= += -= *= /=), Increment/Decrement (++ --), Bitwise (& | ^ ~ << >>), and the Conditional/Ternary operator (?:). Operator precedence and associativity determine the order sub-expressions are evaluated in a complex statement.
Key operator nuances to master:
- Modulus Operator (
%): Returns the remainder of integer division (e.g.10 % 3 = 1). Modulus cannot be applied to floating-point numbers. - Logical Short-Circuiting: For
&&, if the left operand evaluates to false, the right operand is never evaluated. For||, if the left operand evaluates to true, evaluation terminates early. - Bitwise Operators: Operate directly at binary register level:
&(AND),|(OR),^(XOR),~(NOT/Complement),<<(left shift = multiply by powers of 2),>>(right shift = divide by powers of 2).
int a = 10, b = 3;
printf("%d", a % b); // 1 (modulus: remainder)
printf("%d", a > b ? 1:0); // 1 (ternary: a>b is true)
printf("%d", a << 1); // 20 (bitwise left shift = a*2)