Building Blocks of C
The words of the languageJust as a sentence is made of words and punctuation, a C program is made of tokens: keywords, names, constants, strings, operators and separators. In the lab below you watch the compiler split a line into tokens, check whether a name is allowed, and see what happens to comments and spaces.
- Tokensthe smallest meaningful pieces
- Keywords32 reserved words
- Namesletters, digits, _ — not a digit first
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
The ideas behind the lab
Exactly what you watched happen, in plain steps.
Six kinds of tokens
The compiler first splits your program into tokens — the smallest pieces that mean something, like words and punctuation in a sentence. Every token is one of six kinds. Spaces, tabs, new lines and comments are not tokens: they only separate tokens, or are thrown away.
keyword int return if while for
identifier total main printf price_2
constant 10 3.14 'A' 0x1F
string "Hello, World!"
operator = + * == && ++
separator ; , ( ) { } [ ]
| Group | Keywords | Count | You meet them in |
|---|---|---|---|
| Data types | char int float double void short long signed unsigned | 9 | Data, Variables |
| Qualifiers | const volatile | 2 | Variables, Pointers |
| Storage classes | auto static extern register typedef | 5 | Scope & storage, Structures |
| Decisions | if else switch case default | 5 | Decisions |
| Loops | for while do break continue | 5 | Loops |
| Jumps | return goto | 2 | Functions, Loops |
| Your own types | struct union enum | 3 | Your own types |
| Operator | sizeof | 1 | Variables, Operators |
The C character set
- Letters: A–Z and a–z. C is case-sensitive, so A and a are different.
- Digits: 0–9.
- Special characters: + - * / % = < > ! & | ^ ~ ? : ; , . ( ) { } [ ] # ' " \ _
- Whitespace: space, tab and new line — they separate tokens.
- Other symbols such as @ ` or $ may appear only inside strings and comments.
Rules for identifiers (names)
- Use only letters, digits and the underscore _.
- The first character must be a letter or _ — never a digit.
- No spaces, hyphens or other symbols: my_score, not my-score or my score.
- A keyword cannot be a name: int, while, return … are taken.
- Upper and lower case differ: total, Total and TOTAL are three names.
Comments, spaces and statements
- // comment runs to the end of the line; /* comment */ can cover many lines.
- Comments are removed before compiling, and cannot be nested.
- Extra spaces, tabs and blank lines are ignored — use them to make code readable.
- A statement is one instruction and ends with ; (a semicolon).
- { } group statements into a block, e.g. the body of main.
Every token of a small program
Every line, explained.
/* Price of 3 books */
A comment — removed before compiling, so it produces no tokens at all.
#include <stdio.h>
A preprocessor directive (# include) with a header name <stdio.h>. No semicolon: it is not a statement.
int main(void)
keyword int · identifier main · separator ( · keyword void · separator ) — 5 tokens.
{
Separator: opens the block that is the body of main.
int qty = 3;
keyword int · identifier qty · operator = · constant 3 · separator ; — 5 tokens.
float price = 9.5f;
9.5f is ONE token: a floating-point constant with the f (float) suffix.
float total = qty * price; // 28.5
Spaces around * are optional; the // comment disappears. 7 tokens.
printf("%.1f\n", total);
identifier printf · ( · string literal "%.1f\n" (one token, however long) · , · identifier total · ) · ;
return 0;
keyword return · constant 0 · separator ;
}
Separator: closes the block.
Spot the bug
Mistakes almost every beginner makes — and how to fix them.
Using a keyword as a name
int float = 2.5; int return = 1;
float rate = 2.5; int result = 1;
Keywords are reserved by C. The compiler stops with "expected identifier".
A name that starts with a digit
int 2nd_place = 20;
int second_place = 20;
C reads 2nd_place as a (broken) number: "invalid suffix nd_place on integer constant".
Hyphens or spaces in names
int my-score = 5; int my score = 5;
int my_score = 5; int myScore = 5;
my-score means "my minus score", and my score is two names. Join words with _ or capital letters.
Capital letters in keywords
Int main(void)
{
Return 0;
}
int main(void)
{
return 0;
}
All keywords are lower-case. Int and Return are just unknown names to the compiler.
Comments inside comments
/* old code: x = 5; /* set x */ y = 6; */
/* old code: x = 5; // set x y = 6; */
The first */ ends the comment, so y = 6; */ becomes code and causes errors. /* comments cannot be nested.
Check your understanding
Answer each question — you will see the explanation straight away.
How many tokens are in: int x = 10;
int · x · = · 10 · ; — five tokens. Spaces are not tokens.
Which is a valid identifier?
_count starts with an underscore and uses only allowed characters. The others start with a digit, contain - or are a keyword.
How many keywords did the original C standard (C89) have?
C89/C90 has 32 keywords. Later standards added a few more, such as inline and _Bool.
What happens to comments when you compile a program?
The preprocessor replaces each comment with a space; the compiler never sees them.
In "Hello, World!", how many tokens are there?
A string literal is a single token, no matter how many characters or spaces it contains.
Building Blocks of C quiz
Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.