Tokens & Keywords
Before the compiler can understand your program's meaning, it first breaks the source text into tokens — the smallest units that still carry meaning on their own. Understanding tokens explains why certain typos produce confusing errors, and why some words simply cannot be used as variable names.
What counts as a token
Token category | Examples |
|---|---|
Keywords |
|
Identifiers |
|
Constants |
|
String literals |
|
Operators |
|
Punctuators |
|
A line like int total = a + b; is tokenized as: the keyword int, the identifier total, the operator =, the identifier a, the operator +, the identifier b, and the punctuator ; — seven tokens in total, with whitespace between them discarded once it has served its purpose of separating tokens.
The C keywords
Keywords are reserved words with a fixed meaning built into the language. The standard C keyword set (C11) is deliberately small:
Category | Keywords |
|---|---|
Type keywords |
|
Control flow keywords |
|
Storage class keywords |
|
Qualifiers & other keywords |
|
Identifiers
An identifier is a name you choose for a variable, function, type, or label. Identifiers may contain letters, digits, and underscores, but cannot start with a digit, and are case-sensitive (total and Total are different identifiers).
myVariable,_count,MAX_SIZE,row2— all valid identifiers2ndValue— invalid, cannot start with a digitmy-variable— invalid, hyphens are not allowed in identifiers
int int = 5; /* error: expected identifier before 'int' */ double return = 2; /* error: expected identifier before 'return' */