Open In App

C++ Identifiers

Last Updated : 06 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++ programming language, identifiers are the unique names assigned to variables, functions, classes, structs, or other entities within the program. Let’s take a look at an example:

C++
// Creating a variable
int val = 10;

// Creating a function
void func() {}

In the above code, the words val and num are identifiers. Basically, everything named by a programmer is an identifier and is used to refer to the entity later in the program.

Rules to Name of 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.
  • 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.

To know more about identifiers naming rules, refer to this article – Naming Convention in C++

examples of valid and invalid identifiers

Example of Valid/Invalid Identifiers

Example

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++
//Driver Code Starts{
#include <iostream>
using namespace std;

//Driver Code Ends }

// 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);

//Driver Code Starts{

    return 0;
}
//Driver Code Ends }

Output
The sum is: 12

Identifier Naming Conventions

Naming conventions are not the rules enforced by C++ language but are suggestions for naming variables by the programming community for easier understanding. Some of the naming conventions are as follows:

For Variables:

  • Use camelCase (for constants, you can use UPPER_SNAKE_CASE)
  • Start with lowercase alphabet.
  • Use descriptive, meaningful names.
  • For example, frequencyCount, personName

For Functions:

  • Use camelCase.
  • Use Verb or verb phrases for naming.
  • For example, getName(), countFrequency(), etc.

For Classes:

  • Use PascalCase
  • Use Nouns or noun phrases for naming.
  • For example, Car, Person, etc

Once again, above are some suggestions for naming identifiers do not rule. They also depend on the project guidelines you are working on and your preferences.



Next Article
Article Tags :
Practice Tags :

Similar Reads