100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Identifiers in C++

Learn what identifiers are in C++, the naming rules the compiler enforces, and good naming conventions.

BasicsBeginner6 min readJul 7, 2026
Analogies

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 — total and Total are 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

cpp
#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

text
Age: 21
Roll No: 45
Average: 88.5

6. 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

Was this page helpful?

Topics covered

#CStudyNotes#Programming#IdentifiersInC#Identifiers#Syntax#Explanation#Example#StudyNotes#SkillVeris#ExamPrep