Tokens, Keywords, and Identifiers
The C character set, the token kinds the compiler sees, reserved keywords, and the rules for naming identifiers.
The Compiler Reads Words, Not Characters
When the compiler looks at your source, it does not see a stream of characters. It first chops the text into tokens: the words and symbols of the language. Understanding that chopping explains a whole family of rules, from what you may name a variable to why int x; works and in tx; does not. It is also a reliable early exam question, so let us get the vocabulary exact.
The Character Set
ANSI C source is built from a modest alphabet: the letters A to Z and a to z, the digits 0 to 9, the underscore _, whitespace, and a set of punctuation symbols like { } ( ) ; , + *. Everything you write is spelled from this set. Whitespace, spaces, tabs, and newlines, separates tokens but is otherwise ignored: the compiler does not care whether your program is beautifully indented or crushed onto one line. Indent for the humans.
The Six Kinds of Token
Every token the compiler produces is one of six kinds:
- Keywords: words the language reserves for itself, like
int,return,if. - Identifiers: the names you invent for variables and functions, like
principalormain. - Constants: literal values written in the source, like
100or7.5. - String literals: quoted text, like
"Total = %.2f\n". - Operators: symbols that act on values, like
+,=,*. - Punctuation: the structural symbols, like
;,{,},(,).
Take one line from the last chapter and count:
interest = principal * rate * years / 100.0;
Ten tokens: four identifiers, four operators, one constant, one semicolon. Seeing code this way, as a token stream, is how the compiler sees it, and it is the level at which syntax errors happen.
Keywords
ANSI C reserves exactly 32 keywords. You have met a few; the full set is:
auto break case char const continue double default
do else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while
Two things matter about them. First, they are spelled in lower case and C is case-sensitive, so int is a keyword while Int and INT are ordinary identifiers. Second, a keyword can never be an identifier: int if = 3; is a syntax error, not a naming choice. The count of 32, and the list itself, is a routine short-answer exam question; later standards added more (inline in C99, for instance), which is trivia worth knowing but outside this dialect.
Identifiers
An identifier is any name you coin. The rules are strict and small:
- Built only from letters, digits, and underscore.
- Must not begin with a digit:
total2is fine,2totalis not. - Case matters:
total,Total, andTOTALare three different names. - Must not be a keyword.
Two conventions ride on top of the rules. Names starting with an underscore are legal but reserved by convention for the compiler and library, so course code never starts a name with _. And ANSI C only guarantees that the first 31 characters of an internal name are significant (for names shared across files the guarantee drops to 6, case-insensitive, a limit modern linkers ignore but exam papers still cite). Practical advice is unchanged since 1989: choose short, descriptive, lower-case names like interest and rowCount.
Seeing Case-Sensitivity Bite
The classic beginner error is declaring one spelling and using another:
Change the printf line to printf("%d\n", Total); and run it. The compiler answers with an error naming Total as undeclared, even though total sits one line above; to a case-sensitive language they are strangers. Errors of this family, an undeclared identifier that looks declared, are almost always a spelling or case mismatch.
What This Buys You
Tokenizing explains rules that otherwise look arbitrary. Why can whitespace not appear inside a name? Because it would split one token into two. Why can 2total not be a name? Because the tokenizer, seeing a digit first, commits to reading a number and then trips over the letters. Why is int unavailable as a variable name? Because the token stream would become ambiguous. The language's small print is mostly the tokenizer's honesty.
Key Takeaways
- The compiler chops source text into tokens: keywords, identifiers, constants, string literals, operators, punctuation.
- ANSI C has exactly 32 reserved keywords, all lower case, and none may be used as an identifier.
- Identifiers use letters, digits, and underscore; never a leading digit; case-sensitive throughout.
- Leading underscores are reserved by convention; ANSI C guarantees 31 significant characters for internal names.
- Whitespace separates tokens and is otherwise ignored; indentation exists for human readers.
How did you find this lesson?
Your rating helps us improve the content.
Create an account to track your progress and access interactive exercises. Already have one? Sign in.
Tokens, Keywords, and Identifiers - Quiz
Test your understanding of the lesson.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!