Identifiers in C++

In C++, Identifiers are the names used to identify variables, functions, classes, arrays, or any other user-defined elements in a program. They help the compiler and programmer to uniquely recognize different entities in the code.

Identifiers are chosen by the programmer, unlike keywords which are reserved by the language. However, there are certain rules and conventions that must be followed while creating identifiers.

Rules for Identifiers

Important Rules:

  1. Identifiers must begin with a letter (A–Z or a–z) or underscore (_).
  2. They can contain letters, digits, and underscore, but cannot start with a digit.
  3. Identifiers are case-sensitive (Age and age are different).
  4. They cannot be a keyword (e.g., int, while).
  5. Special characters like @, #, $, % are not allowed.
  6. There is no length limit in standard C++, but too long names are not recommended.

Detailed Explanation of Rules

1. Identifiers must begin with a letter or underscore (_)

Every identifier in C++ should start with an alphabet (A–Z or a–z) or an underscore. This ensures the compiler correctly interprets the name.

// Valid
int age;
int _number;

// Invalid
int 1value;   // cannot start with digit

2. They can contain letters, digits, and underscore (but not start with digit)

After the first character, an identifier may include alphabets, numbers, or underscores. Digits are allowed only after the first position.

// Valid
int marks1;
float student_2;

// Invalid
int 9marks;   // starts with digit

3. Identifiers are case-sensitive

C++ treats uppercase and lowercase letters differently. So Age and age are two separate identifiers.

int Age = 20;
int age = 15;
// Both variables are stored separately.

4. Identifiers cannot be a keyword

Keywords are reserved by the C++ language and cannot be reused as identifiers. Using them will cause a compilation error.

// Invalid
int int = 10;     // 'int' is a keyword
float while = 3;  // 'while' is a keyword

5. Special characters are not allowed

Identifiers can only contain alphabets, digits, and underscores. Symbols like @, #, $, % are not valid.

// Invalid
int total$marks;
float #price;

6. No length limit (but avoid too long names)

C++ allows very long identifiers, but it is a good practice to use meaningful and short names for readability.

// Valid (but not recommended)
int this_is_a_very_long_variable_name_used_for_example_purposes = 100;

// Better (recommended)
int totalMarks = 100;

Best Practices for Identifiers

  • Use meaningful names (e.g., studentAge instead of sa).
  • Follow a consistent naming convention (camelCase or snake_case).
  • Keep names short but descriptive.
  • Avoid mixing uppercase and lowercase unnecessarily.

Real-Life Analogy:

Think of identifiers as names of students in a classroom. Each student must have a unique name/roll number so the teacher can identify them. Similarly, in a program, identifiers give unique names to variables, functions, or classes so that the compiler can recognize them.

Example Program Using Identifiers

// C++ program using identifiers
#include <iostream>
using namespace std;

int main() {
    int marks = 90;            // 'marks' is an identifier
    float percentage = 90.5;   // 'percentage' is an identifier

    cout << "Marks: " << marks << endl;
    cout << "Percentage: " << percentage;

    return 0;
}
  

Output:

Marks: 90

Percentage: 90.5

Explanation:

  • marks is the identifier used to store integer value 90.
  • percentage is another identifier for storing floating-point value 90.5.
  • cout is a predefined identifier used to display output on the screen.
  • Both marks and percentage follow identifier naming rules — they begin with a letter, contain no special characters or spaces, and are not keywords.

Valid vs Invalid Identifiers

Valid Identifiers Invalid Identifiers
totalMarks 2value
_counter float (keyword)
roll_no student-name (special character)
Value123 price$

Summary:

  • Identifiers are names given to entities like variables, functions, or classes.
  • They must follow rules (cannot start with digits, no special characters, not a keyword).
  • Identifiers are case-sensitive.
  • Examples of valid identifiers: age, studentName, _total.
  • Examples of invalid identifiers: 1value, class, my-name.
  • Good identifiers improve readability and maintainability of the program.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar