C++ Identifiers

In C++, identifiers are used to refer to the names of the variables, functions, arrays, or other user-defined data types created by the programmer. They are the basic requirement of any language. Every language has its own rules for naming the identifiers.

Syntax:

It has the following syntax:

Example

// Creating a variable

int x = 15;

int y = 20;

// Creating a function

void func() {}

Here, the words x and y are identifiers. However, in the C++ program , everything named by a developer is an identifier. It is utilized to refer to the entity later in the C++ program.

In short, we can say that the C++ identifiers represent the essential elements in a program, which are given below:

  • Constants
  • Variables
  • Functions
  • Labels
  • Defined data types
  • Simple C++ Identifier Example:

Let us look at an example program how identifiers are used in C++ programming :

Example

Example

#include <iostream>

using namespace std; //using standard namespace

// Must begin with a letter (A-Z or a-z) or an underscore (_)

int studentAge = 20;  

// contain letters, digits (0-9), or underscores (_) after the first character

double salary_2024 = 55000.75;  

// Cannot be a C++ keyword (e.g., 'int', 'return', 'class') - Correct usage

const int MAX_LIMIT = 100;  

//Case-sensitive: 'MyVariable' and 'myVariable' are different

int myVariable = 10;

int MyVariable = 15;  

string employee_Name = "Alice";  

void displayStudentDetails() { 

    cout << "Student Age: " << studentAge << endl;

}

int main() {

    // Displaying variable values

    cout << "Student Age: " << studentAge << endl;

    cout << "Salary: $" << salary_2024 << endl;

    cout << "Max Limit: " << MAX_LIMIT << endl;

    cout << "MyVariable: " << myVariable << endl;

    cout << "MyVariable (capitalized): " << MyVariable << endl;

    cout << "Employee Name: " << employee_Name << endl;

    

    displayStudentDetails(); // Calling function

    return 0;

}

Output:

Output

Student Age: 20

Salary: $55000.75

Max Limit: 100

MyVariable: 10

MyVariable (capitalized): 15

Employee Name: Alice

Student Age: 20

Explanation:

This example shows proper C++ identifier syntax by beginning with letters and allowing combinations of letters, digits, and underscores while also excluding keywords and upholding case-sensitive rules. The code defines variables using clear names and shows the values for both functions along with constants.

Rules For C++ Identifiers

Some naming rules for identifiers are common in both C and C++. They are as follows:

  • Only alphabetic characters (A-Z and a-z, digits (0-9), and underscores(_) are allowed.
  • The identifier name cannot start with a digit, i.e., the first letter should be alphabetical. After the first letter, we can use letters, digits, or underscores.
  • In C++, uppercase and lowercase letters are distinct. Therefore, we can say that C++ identifiers are case-sensitive.
  • A declared keyword cannot be used as a variable name.
  • The identifier must not have special elements or spacing within its name, such as my-variable, which fails to meet this rule.
  • A meaningful name system that employs age instead of x works best.
  • Valid Identifiers

The following are examples of valid identifiers:

  • Result
  • Test2
  • _sum
  • power
  • Invalid Identifiers

The following are the examples of invalid identifiers:

  • Sum-1 // containing the special character '-'.
  • 2data //The first letter is a digit.
  • Break// use of a keyword.
  • Note: Identifiers cannot be used as the keywords. It may not conflict with the keywords. We should always use a consistent way to name the identifiers so that our code will be more readable and maintainable.

The major difference between C and C++ is the limit on the name length of the variable. ANSI C considers only the first 32 characters in a name, while ANSI C++ imposes no limit on the length of the name.

Constants are the identifiers that refer to the fixed value, which does not change during the execution of a program. Both C and C++ support various kinds of literal constants, and they do not have any memory location. For example, 123, 12.34, 037, 0X2, etc., are the literal constants.

C++ Identifier Example

Let's look at a simple example to understand the concept of identifiers.

Example

Example

#include <iostream>  

using namespace std;  

int main()  

{  

    int a;  

    int A;  	

    cout<<"Enter the values of 'a' and 'A'";  

    cin>>a;  

    cin>>A;  

    cout<<"\nThe values that you have entered are : "<<a<<" , "<<A;  

    return 0;  

}

Output:

Output

Enter the values of 'a' and 'A'

5

6

The values that you have entered are: 5, 6

Explanation:

In the above code, we declare two variables, 'a' and 'A'. Both the letters are the same, but they will behave as different identifiers. As we know, the identifiers are case-sensitive so that both identifiers will have different memory locations.

Types of Identifiers

In C++, identifiers are used in several cases, such as Variables, Functions, and Classes.

1. Variable Identifiers

In C++, programs use variables as labelled storage spaces that contain data values. We have to follow naming rules during declaration, which helps to prevent programming errors.

  • Use camelCase formatting (for constants, use UPPERSNAKECASE).
  • Begin with a lowercase letter.
  • Use clear and descriptive names that reflect their purpose.

C++ Example for Variable Identifiers:

Let us look at an example of how identifiers are used in Variables.

Example

Example

#include <iostream>

using namespace std; //using standard namespace

int main() {

    int studentAge = 20; // Identifier for a variable

    double piValue = 3.1415; // Identifier for a floating-point variable

    cout << "Student Age: " << studentAge << endl;

    cout << "Value of Pi: " << piValue << endl;

    return 0;

}

Output:

Output

Student Age: 20

Value of Pi: 3.1415

Explanation:

This program contains two valid identifiers which include studentAge for integers and piValue for floating-point data storage.

2. Function Identifiers

In C++, function identifiers act as labels that identify blocks of code that execute particular tasks.

  • Use camelCase for function names.
  • Select verb-based or action-descriptive names to indicate their behaviour.

C++ Function Identifiers Example:

Let us look at an example of how identifiers are used in Functions.

Example

Example

#include <iostream>

using namespace std; //using standard namespace

void displayMessage() { // Function identifier

    cout << "Hello, C++!" << endl;

}

int main() {

    displayMessage() ; // Calling the function identifier

    return 0;

}

Output:

Output

Hello, C++!

Explanation:

In this example, the displayMessage function operates as a block identifier for reusable code segments.

3. Classes Identifiers

Using object-oriented programming the class identifier establishes the structure for program objects.

  • Use PascalCase (each word starts with an uppercase letter).
  • Use nouns or noun phrases to represent objects or entities.

C++ Class Identifier Example:

Let us look at an example of how identifiers are used in classes.

Example

Example

#include <iostream>

using namespace std; //using standard namespace

class Car { // Class identifier

public:

    string brand;

    int year;

};

int main() {

    Car myCar; // Object identifier

    myCar.brand = "Toyota";

    myCar.year = 2022;

    cout << myCar.brand << " " << myCar.year << endl;

    return 0;

}

Output:

Output

Toyota 2022

Explanation:

In this example, the "Car" represents a class identifier, but "myCar" functions as an object identifier.

What are the keywords?

Keywords are the reserved words that have a special meaning to the compiler. They are reserved for a special purpose and cannot be used as identifiers. For example, 'for', 'break', 'while', 'if', 'else', etc. are predefined words where predefined words are those words whose meaning is already known by the compiler.

At the same time, the identifiers are the names that the programmer for the program elements, such as variables, functions, arrays, objects, and classes.

Differences between Identifiers and Keywords

Several differences between identifiers and keywords in C++ are as follows:

Identifiers Keywords
Identifiers are the names defined by the programmer to the basic elements of a program. Keywords are the reserved words whose meaning is known by the compiler.
It is used to identify the name of the variable. It is used to specify the type of entity.
It can consist of letters, digits, and underscore. It contains only letters.
It can use both lowercase and uppercase letters. It uses only lowercase letters.
No special character can be used except the underscore. It cannot contain any special character.
The starting letter of identifiers can be lowercase, uppercase or underscore. It can be started only with the lowercase letter.
It can be classified as internal and external identifiers. It cannot be further classified.
Examples are test, result, sum, power, etc. Examples are 'for', 'if', 'else', 'break', etc.

Conclusion

In conclusion, C++ programming requires identifiers to specify meaningful names for variables and functions as well as classes and additional entities. Programmers who follow standard naming practices along with keyword segregation from constants and labels will produce clear, efficient C++ source code.

C++ identifiers MCQs:

  1. What is the key difference between an identifier and a keyword in C++?
  • Identifiers come during program development, while keywords serve as end-user definitions.
  • Programmers can modify identifiers but the keywords stay immutable.
  • Identifiers function as programmer-defined names, but keywords exist as predefined elements.
  • Keywords serve as variable names, yet identifiers lack this capability in C++.
  1. Which of the following option is NOT a valid rule for naming identifiers in C++?
  • The first character of identifiers needs to be either a letter or an underscore.
  • Identifiers can contain spaces.
  • Identifiers are case-sensitive.
  • The naming rules for identifiers exclude C++ keywords from being used.
  1. What will happen if you declare an identifier with a C++ keyword, like below?
  2. Example
    
    int return = 10;
    
  • The program will run normally.
  • The system considers return as any regular variable type.
  • Compilation error due to invalid identifier usage.
  • The program will print 10.
  1. Identify the incorrect identifier among the following:
  • _tempValue
  • 2ndPlayer
  • userName
  • item_count
  1. What will be the output of the following code?
  2. Example
    
    #include <iostream>
    
    using namespace std;
    
    class Car { // Defining a class named Car  
    
    public:
    
        string brand; // Public member variable to store the car brand  
    
    };
    
    int main() {
    
        Car myCar; // Creating an object of the Car class
    
        myCar.brand = "Tesla"; // Assigning a value to the brand attribute  
    
        cout << myCar.brand << endl;
    
        return 0;
    
    }
    
  • Tesla
  • myCar
  • Compilation error

Input Required

This code uses input(). Please provide values below: