Open In App
Related Articles

C++ Identifiers

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In C++ programming language, identifiers are the unique names assigned to variables, functions, classes, structs, or other entities within the program. For example, in the below statement,

int num = 11;

num is an identifier.

Rules to Name of an Identifier in C++

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

  1. An identifier can consist of letters (A-Z or a-z), digits (0-9), and underscores (_). Special characters and spaces are not allowed.
  2. An identifier can only begin with a letter or an underscore only.
  3. 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 has already some predefined meaning in C++. Attempting to use these as identifiers will result in a compilation error.
  4. Identifier must be unique in its namespace.

Additionally, C++ is a case-sensitive language so the identifier such as Num and num are treated as different.

The below images show some valid and invalid C++ identifiers.

examples of valid and invalid identifiers

Example of Valid/Invalid Identifiers

Example of C++ Identifiers

In this example, we have used the identifiers by following the guidelines and we use identifiers to name a class, function, integer data type, etc. If you are not aware of functions and classes in C++ then don’t you will learn them soon. The below code is run successfully which means we named them correctly.

C++

// C++ program to illustrate the identifiers
#include <iostream>
using namespace std;
  
// here Car_24 identifier is used to refer the below class
class Car_24 {
    string Brand;
    string model;
    int year;
};
  
// calculateSum identifier is used to call the below
// function
void calculateSum(int a, int b)
{
    int _sum = a + b;
    cout << "The sum is: " << _sum << endl;
}
  
int main()
{
    // identifiers used as variable names
    int studentAge = 20;
    double accountBalance = 1000.50;
    string student_Name = "Karan";
  
    calculateSum(2, 10);
  
    return 0;
}

                    

Output
The sum is: 12



Last Updated : 14 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments