C Tokens and Keywords
Every C program you write is made up of small individual units called tokens. Just like a sentence in English is built from words, a C program is built from tokens. The compiler reads your source code and breaks it down into these tokens before processing anything else.
Understanding tokens and keywords gives you a strong foundation for reading and writing C code correctly.
What is a Token?
A token is the smallest meaningful unit in a C program. When the compiler scans your code, it groups characters into tokens and then uses those tokens to understand what the program is supposed to do.
Think of it like reading a recipe: the recipe is the program, individual words and symbols (like "2", "cups", "flour", "+") are the tokens, and the compiler is the chef reading it.
Types of Tokens in C
C has six types of tokens:
- Keywords
- Identifiers
- Constants (Literals)
- Strings
- Operators
- Special Symbols (Punctuators)
// Example program showing various tokens
int main()
{
int age = 25; // int, main, age, =, 25, ;
printf("Hello"); // printf, "Hello"
return 0; // return, 0
}
Token Breakdown of: int age = 25;
| Token | Type |
|---|---|
int | Keyword |
age | Identifier |
= | Operator |
25 | Constant (Integer Literal) |
; | Special Symbol |
1. Keywords
Keywords are reserved words with a fixed meaning in C. The compiler gives each keyword a specific role. You cannot use keywords as variable names or function names — they are permanently reserved by the language.
C has 32 keywords in the C89/C90 standard:
| Column 1 | Column 2 | Column 3 | Column 4 |
|---|---|---|---|
| auto | break | case | char |
| const | continue | default | do |
| double | else | enum | extern |
| float | for | goto | if |
| int | long | register | return |
| short | signed | sizeof | static |
| struct | switch | typedef | union |
| unsigned | void | volatile | while |
Keywords Added in C99 and Later
| Standard | New Keywords |
|---|---|
| C99 | inline, restrict, _Bool, _Complex, _Imaginary |
| C11 | _Alignas, _Alignof, _Atomic, _Generic, _Noreturn, _Static_assert, _Thread_local |
Examples of Keywords in Use
int score = 0; // int is a keyword (data type)
if (score > 50) { // if is a keyword (decision)
return 1; // return is a keyword
}
while (score < 100) { // while is a keyword (loop)
score++;
}
2. Identifiers
An identifier is a name given by the programmer to a variable, function, array, or any other user-defined item. Unlike keywords, identifiers have no predefined meaning — you choose them.
Rules for Writing Identifiers
- Must start with a letter (a–z or A–Z) or an underscore (_)
- Can contain letters, digits (0–9), and underscores
- Cannot contain spaces or special characters like
@,$,! - Cannot be the same as a C keyword
- C identifiers are case-sensitive —
ageandAgeare different
| Valid Identifiers | Invalid Identifiers | Reason Invalid |
|---|---|---|
| age | 2age | Starts with a digit |
| _count | my age | Contains a space |
| totalSum | int | Reserved keyword |
| data_1 | data@1 | Contains special character |
3. Constants (Literals)
A constant (also called a literal) is a fixed value written directly in the code. It does not change during program execution.
| Type | Example | Description |
|---|---|---|
| Integer Constant | 10, -5, 0 | Whole numbers |
| Float Constant | 3.14, -0.5 | Decimal numbers |
| Character Constant | 'A', 'z', '3' | Single character in single quotes |
| Octal Constant | 075 | Starts with 0 (base 8) |
| Hexadecimal Constant | 0x1F | Starts with 0x (base 16) |
int a = 42; // 42 is an integer constant
float pi = 3.14; // 3.14 is a float constant
char ch = 'C'; // 'C' is a character constant
int hex = 0xFF; // 0xFF = 255 in decimal
4. String Literals
A string literal is a sequence of characters enclosed in double quotes. Strings are a special type of token in C.
printf("Hello, World!"); // "Hello, World!" is a string literal
char name[] = "Alice"; // "Alice" is a string literal
Strings are stored as arrays of characters and automatically end with a null character \0.
5. Operators
Operators are symbols that perform operations on values or variables. Each operator is a token.
| Category | Examples |
|---|---|
| Arithmetic | +, -, *, /, % |
| Relational | ==, !=, <, >, <=, >= |
| Logical | &&, ||, ! |
| Assignment | =, +=, -=, *= |
| Bitwise | &, |, ^, ~, <<, >> |
| Increment/Decrement | ++, -- |
6. Special Symbols (Punctuators)
Special symbols are characters that have a specific structural role in C code — they are not operators but they define the shape and structure of the program.
| Symbol | Name | Role |
|---|---|---|
{ } | Curly Braces | Marks the start and end of a code block |
( ) | Parentheses | Used in function calls, conditions, expressions |
[ ] | Square Brackets | Used for arrays |
; | Semicolon | Ends a statement |
, | Comma | Separates items in a list |
# | Hash | Starts a preprocessor directive |
* | Asterisk | Pointer declaration or dereference |
& | Ampersand | Address-of operator |
Token Diagram: Full Program Breakdown
Program:
#include <stdio.h>
int main() {
int x = 10;
return 0;
}
Tokens:
--------------------------
| Token | Type |
--------------------------
| int | Keyword |
| main |Identifier|
| ( | Symbol |
| ) | Symbol |
| { | Symbol |
| int | Keyword |
| x |Identifier|
| = | Operator |
| 10 | Constant |
| ; | Symbol |
| return | Keyword |
| 0 | Constant |
| ; | Symbol |
| } | Symbol |
--------------------------
Case Sensitivity in C
C is a case-sensitive language. This means int, INT, and Int are three completely different tokens. Only the lowercase int is a valid keyword. The others would be treated as identifiers.
int age = 5; // correct
INT age = 5; // wrong — INT is not a keyword
Int age = 5; // wrong — Int is not a keyword
Summary
Tokens are the smallest building blocks of a C program. C has six types of tokens: keywords, identifiers, constants, strings, operators, and special symbols. Keywords are reserved words like int, if, while, and return — they cannot be used as variable names. Identifiers are names you create for variables and functions. Constants are fixed values written directly in code. Operators perform computations, and special symbols define the structure of the program. The compiler breaks every C program into tokens as its first step of understanding your code.
