C++ Identifiers

Last Updated : 13 Jun, 2026

Identifiers in C++ are user-defined names used to represent program elements such as variables, functions, classes, and objects. They provide a way to uniquely refer to different entities within a program.

  • They are used to label and access different components of a C++ program.
  • They must follow certain syntax rules and recommended naming conventions.
C++
#include <iostream>
#include <iostream>
using namespace std;

// Class Identifier
class Student
{
public:

    // Method Identifier
    void show()
    {
        
        // Variable Identifier
        int marks = 90; 
        cout << marks;
    }
};

int main()
{
    // Object Identifier
    Student s1; 
    s1.show();
    return 0;
}

Output
90

This program demonstrates the use of different identifiers in C++, including a class (Student), method (show), variable (marks), object (s1), and function (main). When executed, it creates a Student object and displays the value of the marks variable.

a_

Rules for Naming an Identifier

We can use any word as an identifier as long as it follows the following rules:

  • An identifier can consist of letters (A-Z or a-z), digits (0-9), and underscores (_). Special characters and spaces are not allowed.
  • An identifier can only begin with a letter or an underscore only.
  • C++ has reserved keywords that cannot be used as identifiers since they have predefined meanings in the language. For example, int cannot be used as an identifier as it already has some predefined meaning in C++. Attempting to use these as identifiers will result in a compilation error.
  • An identifier must be unique within its scope (or namespace), meaning you cannot have two identifiers with the same name in the same context.

Additionally, C++ is a case-sensitive language so the identifier such as Num and num are treated as different. The images below show some valid and invalid C++ identifiers.

examples of valid and invalid identifiers
Example of Valid/Invalid Identifiers

Example: Valid and Invalid Identifiers in C++

C++
#include <iostream>
using namespace std;

int main() {
    // Valid identifiers
    int age = 20;
    int _count = 5;
    int totalSum = age + _count;

    cout << "Total: " << totalSum << endl;

    // Invalid identifiers (uncommenting these will cause errors)

    // int 2value = 10;   // cannot start with digit
    // int int = 5;       // keyword cannot be used
    // int my value = 10; // spaces not allowed

    return 0;
}

Output
Total: 25

In this example, identifiers are used according to C++ naming rules in variables, class and function.

C++
#include <iostream>
using namespace std;

// Here Car identifier is used to refer to below class
class Car {
    string Brand;
    string model;
    int year;
};

// getSum identifier is used to call the below
// function
void getSum(int a, int b) {
    int _sum = a + b;
    cout << "The sum is: " << _sum;
}

int main() {
  
    // Identifiers used as variable names
    int studentAge = 20;
    double accountBalance = 1000.50;
    string student_Name = "Karan";

    getSum(2, 10);

    return 0;
}

Output
The sum is: 12

Related Article

Naming Convention in C++

Comment