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:
- Identifiers must begin with a letter (A–Z or a–z) or underscore (_).
- They can contain letters, digits, and underscore, but cannot start with a digit.
- Identifiers are case-sensitive (
Ageandageare different). - They cannot be a keyword (e.g.,
int,while). - Special characters like
@, #, $, %are not allowed. - 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.,
studentAgeinstead ofsa). - 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:
marksis the identifier used to store integer value 90.percentageis another identifier for storing floating-point value 90.5.coutis a predefined identifier used to display output on the screen.- Both
marksandpercentagefollow 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.