Tokens vs Identifiers vs Keywords in C++



In C++, tokens, identifiers, and keywords all are fundamental elements of a program. Tokens are the smallest units of code which are combine together to form complete program, where both keywords and identifiers are the types of tokens. The keywords are reserved words in the language, where each provides separate meanings to code and cannot be used as names by the programmer, whereas identifiers are names defined and used by programmers to represent variables, function or other user-defined elements. In this article, we will learn about all three in detail.

Tokens in C++

A token is the smallest element of a C++ program, which is recognized by the compiler. It is like building blocks of a program, where all these individual elements are put together to build a program.

Here is the following list of tokens available in C++, which are put together to frame a whole program.

Example

Here is the following example code showcasing how the different types of tokens are used together to form and build a program.

#include <iostream>   
using namespace std;

int main() {
    int a = 5, b = 10;
    int sum = a + b;
    cout << "Sum: " << sum << endl;
    return 0;
}

Output

Sum: 15

Tokens in the above code;

Token Token type
int, return Keyword
main, a, b, sum, Standard library identifiers (cout,endl) Identifiers
5, 10 Literals
+, =, << Operators
; , { , } Punctuators

Identifiers

An identifier is one of the types of tokens, which is said to be the unique name given by a user. For example, method names, variable names, struct names, class names, etc, which are given by a user, are all identifiers that can be uniquely identified.

It also has three sub-categories.

User-defined Identifiers

When the programmer gives names to any variables, functions, classes, etc, then it is said to be user-defined identifiers. There are basic sets of rules written in C++ for defining the names of identifiers.

int age;        // 'age' is a user-defined variable identifier
string name1;    // 'name1' is a user-defined variable identifier

Standard Library Identifiers

These are the pre-defined identifiers, which come from the C++ Standard Library (part of the std namespace). These are basically used for common tasks like input/output, data manipulation, etc.

cout, cin, endl    // Standard Library identifiers for input and output 
vector, map, list  // for standard data structures
string, iostream   // header files in the C++ Standard Library.

Predefined Identifiers

These are predefined identifiers by the C++ language, which are already pre-built in your program, and you can directly use them without explicitly defining them.

main  // predefined identifier which shows the entry point of a C++ program
nullptr  // predefined identifier for null pointer
true, false  // predefined identifier for Boolean literals

Keywords in C++

The keywords are reserved words in the C++ programming language that cannot be used as names for variables or as identifiers in a program. These are part of C++ syntax and are case-sensitive.

Example

#include <iostream>   
using namespace std; 

int main() {         
    int number = 10;  
    if (number > 5) { 
        cout << "Number is greater than 5" << endl;
    }

    return 0;        
}

Here,
using, namespace, int, if, and return are keywords that are reserved and predefined words in C++ programming, and you cannot use these words as names for identifiers, as they are part of the C++ syntax.

number is an identifier.

Output

Number is greater than 5
Akansha Kumari
Akansha Kumari

Hi, I am Akansha, a Technical Content Engineer with a passion for simplifying complex tech concepts.

Updated on: 2025-05-28T19:05:08+05:30

865 Views

Kickstart Your Career

Get certified by completing the course

Get Started