C++ Tokens Overview
In C++, the smallest unit of a program that is meaningful to the compiler is called a token. Just like sentences in English are made up of words, a C++ program is made up of tokens. Without tokens, we cannot form valid instructions in C++.
Tokens are the building blocks of a C++ program. Every valid C++ statement is created by combining different tokens such as keywords, identifiers, constants, operators, and symbols.
Types of Tokens in C++:
| Token Type | Description | Example |
|---|---|---|
| Keywords | Reserved words with special meaning in C++ | int, float, if, while |
| Identifiers | Names given to variables, functions, classes, etc. | marks, total, sum |
| Constants & Literals | Fixed values that do not change during execution | 10, 3.14, 'A', "Hello" |
| Operators | Symbols used to perform operations | +, -, *, /, == |
| Punctuators (Separators) | Symbols that separate statements and group code | ;, {}, (), [] |
Note:
Comments are not tokens in C++. They are completely ignored by the compiler and are only written to make the program more readable for humans. To study comments in detail (with types and examples), click here.
Example:
#include <iostream>
using namespace std;
int main() {
int num = 10; // Here 'int', 'num', '=', '10', ';' are tokens
cout << "num is: " << num;
return 0;
}
Output:
num is : 10
Explanation of Tokens in Example:
- int → Keyword
- num → Identifier
- = → Operator
- 10 → Constant
- ; → Punctuator
Real-Life Analogy:
Just like a sentence is made of words that give it meaning, a C++ program is made of tokens that give instructions to the computer.
Summary:
- Tokens are the basic building blocks of a C++ program.
- Tokens are the smallest units of a C++ program.
- They include keywords, identifiers, constants, operators, and punctuators.
- Every valid C++ instruction is formed by combining these tokens.
- Each has a special role in writing a C++ program.