CTokens & Keywords

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

int, return, while, struct

Identifiers

total, main, myCounter, _temp

Constants

42, 3.14, 0x1F, `'A'

String literals

"Hello, World!"

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

int, char, float, double, void, short, long, signed, unsigned, _Bool, struct, union, enum

Control flow keywords

if, else, switch, case, default, for, while, do, break, continue, goto, return

Storage class keywords

auto, static, extern, register, typedef, _Thread_local

Qualifiers & other keywords

const, volatile, restrict, sizeof, inline, _Atomic, _Generic, _Noreturn, _Static_assert, _Alignas, _Alignof, _Complex, _Imaginary

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 identifiers

  • 2ndValue — invalid, cannot start with a digit

  • my-variable — invalid, hyphens are not allowed in identifiers

Keywords cannot be used as identifiers
Because keywords have a fixed meaning to the compiler, you cannot reuse one as a variable or function name. Trying to compile the following fails immediately:

C
int int = 5;      /* error: expected identifier before 'int' */
double return = 2; /* error: expected identifier before 'return' */
If you get an error like "expected identifier" on a line where you clearly wrote one, check whether the name you picked happens to be a reserved keyword.