What Is a Token?
Take the sentence "The cat sat on the mat." — you instinctively read it as 6 separate words, not one long blob of letters. A token is that same idea applied to C code: the smallest unit that still means something on its own. The compiler doesn't read your code letter by letter either — it breaks it into tokens first, exactly like you break a sentence into words before understanding it.
Every single line of C code is built from exactly 5 kinds of tokens:
- Keywords — reserved words with a fixed meaning (
int,if,return) - Identifiers — names you invent (variables, functions)
- Constants / Literals — fixed values written directly (
25,'A',"Hi") - Operators — symbols that perform an action (
+,=,>) - Punctuators / Special symbols — structural glue (
;,{ },( ),,)
Keywords — Reserved Words You Can't Rename
int, if, while, and return already have a fixed job in the language, so you can't use them as variable names.int main() { int age = 20; if (age >= 18) { return 1; } return 0; } // int, if, return are KEYWORDS — fixed meaning, cannot be renamed // This will NOT compile: // int if = 5; // "if" is reserved, illegal as a variable name
| Category | Examples |
|---|---|
| Data types | int, float, char, double, void |
| Control flow | if, else, switch, case, for, while, do |
| Jump statements | break, continue, return, goto |
| Storage/modifiers | const, static, extern, unsigned, signed |
Identifiers — Names You Choose
totalPrice, studentCount, calculateArea are all identifiers you invented.int calculateArea(int length, int width) { int totalArea = length * width; return totalArea; } // calculateArea, length, width, totalArea are all IDENTIFIERS // Rules: letters, digits, underscore only — can't start with a digit // 2total -> ILLEGAL (starts with a digit) // total_2 -> LEGAL // int -> ILLEGAL as an identifier (it's a keyword!)
Constants / Literals — Fixed Values
₹299 — fixed, printed, not changing based on context. Any value written directly into your code — a number, a single character, a piece of text — is a constant/literal for the exact same reason: it's fixed at that exact spot in the code.int age = 25; // 25 -> integer constant float price = 99.50; // 99.50 -> floating constant char grade = 'A'; // 'A' -> character constant char name[] = "Ravi"; // "Ravi" -> string literal
| Constant type | Example |
|---|---|
| Integer constant | 25, -10, 0 |
| Floating constant | 99.50, 3.14 |
| Character constant | 'A', '7', '\n' |
| String literal | "Ravi", "Hello" |
Operators — Symbols That Do Work
+, =, and > aren't just symbols sitting there; each one tells the compiler to actually do something — add two numbers, store a value, or compare two things.int a = 10, b = 3; int sum = a + b; // + -> arithmetic operator int isBigger = a > b; // > -> relational operator int result = (a > b) && (b > 0); // && -> logical operator
| Category | Examples |
|---|---|
| Arithmetic | + - * / % |
| Relational | > < >= <= == != |
| Logical | && || ! |
| Assignment | = += -= *= /= |
Punctuators — The Structural Glue
;, { }, ( ), , — do exactly this job: ; ends a statement, { } groups a block, ( ) wraps function parameters.void greet(char name[], int age) // ( ) wraps parameters, , separates them { // { starts the function block printf("Hi %s, age %d\n", name, age); // ; ends the statement } // } ends the function block
| Punctuator | Job |
|---|---|
; | Ends a statement |
{ } | Groups a block of code (function body, loop body) |
( ) | Wraps function parameters or groups expressions |
, | Separates items — parameters, array values, variables |
[ ] | Array indexing/declaration |
Breaking Down One Full Line
Here's every token type from this lesson, all inside a single statement: int age = 25;
Quick Quiz
What is a token in C?
Why can't you name a variable "if"?
In int totalArea = 50;, what kind of token is totalArea?
Which of these is a character constant?
What job does the semicolon (;) do as a punctuator?
Lesson Checklist
- I can explain what a token is in my own words
- I can name the 5 token types
- I understand why keywords can't be used as variable names
- I can distinguish an identifier from a keyword
- I can identify integer, float, character, and string constants
- I can spot operators and punctuators in a line of code
- I can break a full statement into its individual tokens
- I completed the quiz