Reading C: tokens, expressions, and statements
Recognize the small language units inside a line of C, then connect them to declarations, expressions, and statements.
In this chapter8 sections
Before reading a full program, learn to recognise its smaller pieces. C groups written characters into tokens, then combines those tokens into declarations, expressions, and statements. This is how familiar punctuation gains a precise meaning.
1. Tokens are the language units
int count = 3;
int total = count + 1;A token is one recognised piece of C source after preprocessing. A whole name such as total is one token. So is ==, even though it contains two characters. In int total = count + 1;, each name, constant, and punctuator has a separate job.
| C17 category | Examples | Role |
|---|---|---|
| Keyword | int, if, return | Reserved spellings with grammatical roles |
| Identifier | count, total | A name for an entity |
| Constant | 1, 2.5, 'A' | A constant token with a type and value |
| String literal | "hello" | Quoted character sequence, a separate token category |
| Punctuator | ;, {, +, == | Structural symbols, many also used as operators |
“Operator” describes a role, not a sixth lexical token category in this C17 list. The + punctuator can act as an addition operator. Preprocessing has its own token rules, including preprocessing numbers; do not assume every intermediate preprocessing token is already a valid final C constant.
2. Names are chosen within rules
For your first portable names, use basic letters, digits, and underscores, and begin with a letter. Choose a descriptive name such as meal_count. Some names are reserved by C and its library; avoiding keywords alone is not the whole rule. Wider character use has version-specific requirements.
int score = 2;
int Score = 3;C distinguishes case, so these are two names for two objects. That example demonstrates a rule, not a recommendation to make names differ only by capitalization. main is a special hosted startup function name, but it is not a C keyword. Recognizing a familiar name does not tell you its lexical category.
3. An expression participates in computation
An expression can compute a value, refer to an object or function, or cause side effects. count + 1 computes a value. count = 4 changes an object. A function call is an expression too. Some expressions have type void and do not provide an ordinary result value.
Read a symbol in its surrounding construct. * can mean multiplication in an expression or help declare a pointer. Parentheses can group an expression or mark a function call. Knowing the shape is a start; knowing the context tells you its job.
4. Statements and declarations are different block items
An expression followed by a semicolon can form an expression statement: count = count + 1;. A declaration such as int count = 3; has a different role. C function blocks allow both declarations and statements. Sharing a semicolon does not make them the same construct.
A compound statement uses braces to group block items. Braces also occur in other contexts, such as initialization, so “every brace starts a function block” is not a reliable rule. A preprocessing directive such as #include <stdio.h> has a different role again; it is not an ordinary statement to which you append a semicolon.
5. Formatting helps people, but some whitespace matters
A statement can span lines, and a line can contain several statements. Indentation helps a person see structure. Spaces can also separate tokens: int count and intcount are different source. Inside a literal, spaces can be part of the stored text.
Newlines terminate preprocessing directives, and comment replacement and line splicing happen according to translation rules. Formatting is therefore neither “all meaningful” nor “completely irrelevant.” Consistent indentation and spacing help a learner see the structure that the grammar actually defines.
In int total = count + 1;, is total a keyword? Is the whole line an expression statement?
Read the explanation
No to both. total is an identifier. The line is a declaration with an initializer; count + 1 is an expression within that initializer.
Primary references and further reading
- WG14 N1570 — public C11 committee draft: Sections 5.1.1.2, 6.4, 6.5, 6.7, and 6.8 provide the core rules used in this C17-baseline explanation. This is a public draft, not the final ISO publication.
Before you move on
- C recognises tokens and combines them according to grammar.
- Expressions, declarations, and statements can share punctuation while doing different jobs.
- Whitespace can separate tokens, and literal text can contain spaces that are data.
Keep these ideas separate
- Token category / Operator role
- A punctuator such as + can act as an operator; lexical category and grammatical job are different descriptions.
How would you separate the parts of int total = count + 1; without calling the whole line an expression?
Optional: say it aloud or keep a note. You can continue whenever you are ready.Check your understanding
Optional. Answers are not saved or sent, and reading stays unrestricted.
Read the explanation for every choice
- In expressions, = assigns and == compares for equality.
✓ Correct reasoning. Their expression roles differ. The = token also appears in initialization syntax, where it is not an assignment expression.
- Whitespace decides whether both mean assignment.
Whitespace does not make these two operators interchangeable.
- The compiler chooses their meaning from the programmer’s intention.
The language’s token and expression rules determine their roles.
C17
Quick reference from the earlier edition
This compact reference keeps existing links and saved passages available. Read the expanded lesson above for the full context.
C source code is composed of a sequence of tokens, which are the smallest meaningful units in the language. The primary token types are:
- Keywords: Reserved words such as
int,if, andreturn. - Identifiers: Names for objects, functions, types, and other entities.
- Constants: Integer, floating, enumeration, and character constants, for example
42,3.14, and'A'. - String literals: Quoted character sequences such as
"hello"; a distinct token category. - Punctuators: Symbols such as
;,{,},+,*, and&&. Many punctuators act as operators; “operator” is not a separate C17 lexical token category.
Whitespace normally separates tokens, and each comment is replaced by one space during translation. Whitespace inside literals is data; newlines terminate preprocessing directives, and whitespace also matters in some macro definitions. Do not assume that deleting every space or comment leaves a program unchanged.
Ready for the next idea?
Mark this chapter as read whenever you feel ready.