BLOG DETAILS

Read the complete blog details covering in-depth explanations, practical insights, and examples. Designed for computer science learners and technology enthusiasts who want clarity, guidance, and knowledge in simple, structured form.

Understanding Tokens in Programming: The Building Blocks of Code

What is a Token in C Programming?

Before the compiler can execute a C program, it first reads and analyzes the source code. During this process, the program is divided into small meaningful parts that the compiler can understand. These smallest meaningful parts are called tokens. In simple words, a token is the basic building block of every C program.

Whether you write a simple program or a large software application, every line of C code is made up of different types of tokens such as keywords, identifiers, constants, string literals, operators, and special symbols. The compiler recognizes these tokens before checking the syntax and generating machine code.

Definition: A token is the smallest meaningful unit of a C program that is recognized by the compiler during the lexical analysis phase.

How Does the Compiler Identify Tokens?

The process of identifying tokens takes place during the Lexical Analysis phase, which is the first stage of the compilation process. In this phase, the lexical analyzer (scanner) reads the source code from left to right and groups individual characters into meaningful units called tokens.

While scanning the program, the lexical analyzer ignores unnecessary elements such as spaces, tabs, blank lines, and comments because they do not affect the execution of the program. Only meaningful elements are converted into tokens.

Exam Point: The Lexical Analysis phase is the first phase of the compiler. Its main responsibility is to convert the source program into a sequence of tokens.

Example of Tokens in C

Consider the following C statement:

int age = 20;

The compiler does not treat the entire statement as one unit. Instead, it breaks the statement into separate tokens, as shown below.

Program ElementToken Type
intKeyword
ageIdentifier
=Operator
20Integer Constant
;Special Symbol (Separator)
Total Tokens = 5

Types of Tokens in C Programming

Every valid C program is formed using different types of tokens. Based on their purpose, tokens in the C programming language are divided into the following six categories.

1. Keywords

Keywords are reserved words provided by the C language. They have predefined meanings and cannot be used as variable names, function names, or identifiers.

Examples: int, float, if, return, while

2. Identifiers

Identifiers are user-defined names used to represent variables, functions, arrays, structures, unions, and other program elements.

Examples: age, marks, total, main

3. Constants

Constants are fixed values that remain unchanged throughout the execution of a program.

Examples: 10, 3.14, 'A'

4. String Literals

A string literal is a sequence of characters enclosed within double quotation marks.

Examples: "Hello", "Suraku Academy"

5. Operators

Operators are special symbols that perform operations such as arithmetic, comparison, assignment, logical operations, increment, decrement, and more.

Examples: +, -, *, =, ==, &&

6. Special Symbols (Separators)

Special symbols define the structure of a C program and separate different parts of the code.

Examples: ;, ,, (), {}, []

Types of Tokens in C Programming

Quick Revision

  • A token is the smallest meaningful unit of a C program.
  • Every C program is made up of one or more tokens.
  • Tokens are identified during the Lexical Analysis phase.
  • Spaces, tabs, blank lines, and comments are ignored during tokenization.
  • The six main types of tokens are Keywords, Identifiers, Constants, String Literals, Operators, and Special Symbols.

Summary of All Token Types

The following table provides a quick overview of all token types used in C programming. It is especially useful for revision before competitive examinations and interviews.

Token TypePurposeExample
KeywordReserved words with predefined meanings.int, char, return
IdentifierUser-defined names for variables, functions, arrays, etc.sum, marks, main
ConstantFixed numeric or character values.100, 3.14, 'A'
String LiteralSequence of characters enclosed within double quotes."Hello"
OperatorPerforms arithmetic, logical, relational, assignment and other operations.+, =, ==, &&
Special SymbolSeparates and organizes program elements.(), {}, [], ;, ,
Quick Tip: In competitive exams, most questions ask you to identify the type of a given token or count the total number of tokens in a program.

Rules for Counting Tokens

Many students make mistakes while counting tokens because they confuse characters with tokens. Remember that a token represents a meaningful unit, not individual letters.

Important Rules
  • Each keyword is counted as one token.
  • Each identifier is counted as one token.
  • Each constant is counted as one token.
  • Each operator is counted as one token.
  • Each special symbol is counted as one token.
  • A complete string enclosed within double quotation marks is considered one string literal token.
  • Spaces, tabs, blank lines and comments are not counted as tokens.
Remember: Never count individual letters or digits separately. Count only meaningful units recognized by the compiler.

Example 1: Count the Number of Tokens

int a = 10;

The compiler divides this statement into the following tokens.

TokenCategory
intKeyword
aIdentifier
=Operator
10Constant
;Special Symbol
Total Tokens = 5

Example 2: Count the Tokens

printf("Hello World");

Although the string contains multiple characters, the compiler considers the complete string as a single string literal token.

TokenCategory
printfIdentifier (Function Name)
(Special Symbol
"Hello World"String Literal
)Special Symbol
;Special Symbol
Total Tokens = 5

Common Mistakes While Counting Tokens

  • Counting each letter of an identifier separately.
  • Counting every character inside a string literal individually.
  • Ignoring operators while counting tokens.
  • Treating spaces or blank lines as tokens.
  • Forgetting that semicolon (;) is also a token.

Exam Strategy

When solving token-counting questions, first separate the statement into meaningful parts. Then classify each part as a keyword, identifier, constant, operator, string literal or special symbol. This approach helps you avoid mistakes and solve questions quickly in examinations.

Solved Examples: Counting Tokens in C Programs

The best way to understand tokens is by practicing different C statements. The compiler always separates the source code into meaningful units before checking the syntax. Let's solve some commonly asked examples.

Example 1

float salary = 25000.50;
TokenCategory
floatKeyword
salaryIdentifier
=Operator
25000.50Floating Constant
;Special Symbol
Total Tokens = 5

Example 2

char grade = 'A';
TokenCategory
charKeyword
gradeIdentifier
=Operator
'A'Character Constant
;Special Symbol
Total Tokens = 5

Example 3

a = b + c * d;
TokenCategory
aIdentifier
=Operator
bIdentifier
+Operator
cIdentifier
*Operator
dIdentifier
;Special Symbol
Total Tokens = 8

Tricky Examples Frequently Asked in Competitive Exams

Some questions appear simple but are designed to confuse students. Understanding these examples will help you avoid common mistakes in examinations.

Example 4

i++;
TokenCategory
iIdentifier
++Increment Operator
;Special Symbol
Total Tokens = 3

Notice that ++ is treated as one operator, not two separate + symbols.

Example 5

if(a >= b)
TokenCategory
ifKeyword
(Special Symbol
aIdentifier
>=Relational Operator
bIdentifier
)Special Symbol
Total Tokens = 6
Important: The operator >= is counted as one token, not two separate tokens (> and =).

Points to Remember for Competitive Exams

  • Keywords always have predefined meanings and cannot be used as identifiers.
  • Identifiers are created by programmers to represent variables, functions, arrays, structures, and other program elements.
  • A complete string enclosed within double quotation marks is counted as one string literal token.
  • Operators such as ++, --, ==, !=, &&, ||, >=, and <= are single tokens.
  • Comments and white spaces are ignored during lexical analysis.
  • Semicolon (;), comma (,), parentheses, braces, and brackets are also counted as tokens.

Quick Revision

Whenever you are asked to count tokens in a C program, divide the statement into meaningful parts instead of individual characters. This simple technique helps you solve token-counting questions quickly and accurately in exams like UGC NET, SET, PGT, TGT, DSSSB, Computer Instructor, and other Computer Science competitive examinations.

Important Exam Points

  • Token is the smallest meaningful unit of a C program.
  • Lexical Analysis is the first phase of the compiler.
  • Comments and white spaces are ignored during tokenization.
  • Every keyword is a token, but every token is not necessarily a keyword.
  • Operators like ++, --, ==, >=, and && are each counted as one token.
  • String literals enclosed within double quotation marks are counted as one token.

Conclusion:

Tokens are the basic building blocks of every C program. Before checking the syntax or generating machine code, the compiler first divides the source code into meaningful tokens during lexical analysis. Understanding different types of tokens is essential because they form the foundation of C programming and compiler design.

For competitive examinations such as UGC NET, SET, PGT, TGT, Computer Instructor, DSSSB, university examinations, and technical interviews, questions related to tokens are frequently asked. Therefore, students should understand not only the definition of tokens but also how to identify and count them correctly in different C statements.

Final Revision

  • Token = Smallest meaningful unit of a C program.
  • Identified during the Lexical Analysis phase.
  • Six main types of tokens are Keywords, Identifiers, Constants, String Literals, Operators, and Special Symbols.
  • Comments and white spaces are ignored.
  • Practice token-counting questions regularly to improve accuracy in competitive exams.
Keep Practicing!

The more C programs you read and write, the easier it becomes to recognize different types of tokens. Regular practice with examples and previous year questions will strengthen your understanding and help you solve exam questions with confidence.

Suresh Kulahry

About the Author

Suresh Kulahry

Suresh Kulahry is the Founder of Suraku Academy and an educator dedicated to helping students prepare for Computer Science examinations through simple explanations, high-quality study material, MCQs, Previous Year Questions, and practical tutorials. His mission is to make computer science learning easy, accessible, and free for every learner.

🎉 Thank You for Reading!

We hope this article helped you understand the topic better. Continue your Computer Science preparation with more free resources available on Suraku Academy.