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:

  1. Keywords
  2. Identifiers
  3. Constants (Literals)
  4. Strings
  5. Operators
  6. 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;

TokenType
intKeyword
ageIdentifier
=Operator
25Constant (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 1Column 2Column 3Column 4
autobreakcasechar
constcontinuedefaultdo
doubleelseenumextern
floatforgotoif
intlongregisterreturn
shortsignedsizeofstatic
structswitchtypedefunion
unsignedvoidvolatilewhile

Keywords Added in C99 and Later

StandardNew Keywords
C99inline, 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 — age and Age are different
Valid IdentifiersInvalid IdentifiersReason Invalid
age2ageStarts with a digit
_countmy ageContains a space
totalSumintReserved keyword
data_1data@1Contains 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.

TypeExampleDescription
Integer Constant10, -5, 0Whole numbers
Float Constant3.14, -0.5Decimal numbers
Character Constant'A', 'z', '3'Single character in single quotes
Octal Constant075Starts with 0 (base 8)
Hexadecimal Constant0x1FStarts 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.

CategoryExamples
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.

SymbolNameRole
{ }Curly BracesMarks the start and end of a code block
( )ParenthesesUsed in function calls, conditions, expressions
[ ]Square BracketsUsed for arrays
;SemicolonEnds a statement
,CommaSeparates items in a list
#HashStarts a preprocessor directive
*AsteriskPointer declaration or dereference
&AmpersandAddress-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.

Leave a Comment

Your email address will not be published. Required fields are marked *