1. Intro
An identifier is the name you give to a program element — a variable, function, class, object, or label — so it can be referred to elsewhere in the code. Every time you write int age;, the word age is an identifier you have created.
Cricket analogy: Naming a new signing 'Rohit' on the team roster is like declaring an identifier — the name age in int age; is the label you've created to refer to that variable later.
2. Syntax
C++ enforces a strict set of rules for what counts as a valid identifier:
Cricket analogy: Just as the ICC has strict rules for what counts as a valid team jersey number, C++ enforces strict rules for what counts as a valid identifier name.
- Can contain letters (a-z, A-Z), digits (0-9), and underscores (_)
- Must start with a letter or an underscore — never a digit
- Cannot contain spaces or special characters like @, #, %, or -
- Cannot be a C++ keyword (e.g.
int,class,return) - Are case-sensitive —
totalandTotalare two different identifiers - Have no fixed length limit in the standard, though extremely long names hurt readability
3. Explanation
These rules exist so the compiler can unambiguously tell an identifier apart from a keyword, a literal, or an operator while tokenizing. _score, score1, and total_amount are all valid identifiers, while 1score (starts with a digit), total-amount (contains a hyphen), and int (a keyword) are all invalid.
Cricket analogy: A jersey numbered '1Kohli' (starting with a digit) or 'V-Kohli' (with a hyphen) would be rejected by the scoring system, just as 1score and total-amount are invalid C++ identifiers, while _score is valid.
Beyond what the compiler allows, good identifier naming is a core readability practice. Descriptive names like studentCount are far easier to maintain than terse names like x or sc, especially as programs grow. Common conventions include camelCase (totalMarks), snake_case (total_marks), and PascalCase for class names (StudentRecord).
Cricket analogy: Naming a variable studentCount instead of sc is like a scorer writing 'Sachin Tendulkar' in full on the scoreboard instead of an ambiguous 'ST', making the record easier to follow years later.
Avoid identifiers that start with an underscore followed by an uppercase letter (like _Value) or contain a double underscore (__count) — these patterns are reserved for the compiler and standard library implementation.
4. Example
#include <iostream>
using namespace std;
int main() {
int studentAge = 21; // valid: starts with a letter
int _rollNumber = 45; // valid: starts with an underscore
double average_score = 88.5; // valid: snake_case
cout << "Age: " << studentAge << endl;
cout << "Roll No: " << _rollNumber << endl;
cout << "Average: " << average_score << endl;
return 0;
}5. Output
Age: 21
Roll No: 45
Average: 88.56. Key Takeaways
- An identifier is a programmer-chosen name for a variable, function, class, or other program element.
- Identifiers may use letters, digits, and underscores, but must not start with a digit.
- Identifiers cannot match a reserved keyword and are case-sensitive.
- Descriptive, consistent naming (camelCase, snake_case, PascalCase) makes code far more maintainable.
Practice what you learned
1. Which of the following is a valid C++ identifier?
2. Are C++ identifiers case-sensitive?
3. Which character can an identifier NOT start with?
4. Why can't a keyword be used as an identifier?
5. Which naming convention writes class names like `StudentRecord`?
Was this page helpful?
You May Also Like
Tokens in C++
Learn what tokens are in C++, the six kinds of tokens, and how the compiler breaks source code into them.
Keywords in C++
Learn what keywords are in C++, why they are reserved, and see the most commonly used ones with examples.
Variables in C++
Learn what are variables in C++, types of variables, declaration, initialization and examples with programs.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics