Identifiers in C++
In C++, an identifier is the name given to a variable, function, class, object, array, or any other user-defined element. Simply put, identifiers help you identify different parts of your program.
Whenever you create a variable like age or a function like calculateSum(), you are creating an identifier. Choosing meaningful names makes your code easier to read and understand.
Example
int age = 20;
float salary = 45000.50;
void displayMessage() {
cout << "Welcome!";
}
In the above example:
ageis an identifier.salaryis an identifier.displayMessageis also an identifier.
The words int, float, and void are keywords, while the names created by the programmer are called identifiers.
Rules for Identifiers in C++
When naming an identifier, you need to follow a few simple rules. If these rules are not followed, the compiler will generate an error.
- An identifier must begin with a letter (
A-Zora-z) or an underscore (_). - It can contain letters, digits, and underscores.
- It cannot begin with a number.
- Spaces are not allowed.
- Special characters such as
@,#,%,&, and$are not allowed. - Keywords cannot be used as identifiers.
- C++ is case-sensitive, so
marksandMarksare treated as different identifiers.
Following these naming rules helps you write error-free and readable programs.
Valid and Invalid Identifiers
The table below shows some examples of valid and invalid identifiers.
| Identifier | Valid or Invalid | Reason |
|---|---|---|
age | ✅ Valid | Starts with a letter |
studentName | ✅ Valid | Uses letters only |
_count | ✅ Valid | Starts with an underscore |
marks123 | ✅ Valid | Contains letters and digits |
2number | ❌ Invalid | Starts with a number |
first name | ❌ Invalid | Contains a space |
class | ❌ Invalid | class is a keyword |
total-price | ❌ Invalid | Hyphen (-) is not allowed |
Naming Identifiers the Right Way
Although you can choose almost any valid name, it is always better to use meaningful identifiers.
For example:
int x = 95;
The above code is valid, but it doesn’t clearly describe what the value represents.
A better approach is:
int marks = 95;
Here, the variable name itself tells you what data is being stored. Good names make your programs easier to understand and maintain.
C++ Identifiers with Examples
Let’s look at a few more examples.
Valid Example
string studentName = "Rahul";
int totalMarks = 450;
float averageMarks = 90.0;
All of these names follow the identifier rules, so they are valid.
Invalid Example
int 1marks = 80;
float total-price = 250.5;
These identifiers are invalid because one starts with a number, while the other contains a special character (-).
Key Points
- Identifiers in C++ are user-defined names given to variables, functions, classes, and other program elements.
- Every identifier should follow the naming rules defined by C++.
- Keywords cannot be used as identifiers.
- Use meaningful names to make your code more readable.
- C++ treats uppercase and lowercase letters differently.
Frequently Asked Questions
Q1. What are identifiers in C++?
Identifiers in C++ are user-defined names used for variables, functions, classes, objects, and other program elements. They help uniquely identify different parts of a program.
Q2. What are the rules for identifiers in C++?
A C++ identifier must begin with a letter or an underscore, can contain letters, digits, and underscores, cannot include spaces or special characters, and must not be a C++ keyword.
Q3. Can identifiers start with a number in C++?
No. An identifier cannot start with a digit. For example, student1 is valid, but 1student is not.
Q4. What is the difference between identifiers and keywords in C++?
Identifiers are names created by the programmer, while keywords are reserved words with predefined meanings that cannot be used as identifiers.
Q5. What are some examples of valid and invalid identifiers in C++?
Examples of valid identifiers include totalMarks, _count, and studentName. Invalid identifiers include 2value, class, and total-marks because they break C++ naming rules or use reserved keywords.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
