Identifiers in C++

A basic idea in C++ and many other programming languages is that of identifiers. Essentially, an identifier is the name assigned to variables, functions, arrays, classes, and other entities. Writing understandable and useful code requires knowing how to define these names correctly, which let us refer to distinct portions of a program.

What are Identifiers?

Programming elements such as variables, functions, classes, objects, and arrays are given user-defined names, or identifiers. For instance, in C++, you may declare an integer variable like this:

int age;

Here, age is an identifier that refers to an integer variable.

Identifiers are references that let you go to and work with the related information or behaviour. Because C++ is a case-sensitive language, age, Age, and AGE would all be regarded as distinct identifiers.

Learn on Youtube:
Identifiers in C++

Rules for Defining Identifiers in C++

C++ imposes specific rules that must be followed when naming identifiers:

Alphabet or Underscore as First Character:

  • Identifiers have to start with an underscore (_) or a letter (A-Z or a-z). An identifier cannot begin with any for the digits 0 through 9.
int age;     // Valid
int _age;    // Valid
int 9age;    // Invalid (cannot start with a number)

Use of Alphanumeric Characters and Underscore:

  • Identifiers can consist of letters, numbers, and underscores after the first character. Special characters like @, #, and spaces are not permitted, though.
float salary_2024;   // Valid
float total$;        // Invalid (special characters not allowed)

Case Sensitivity:

  • Capital and lowercase characters are treated differently as identifiers in C++. For example, count and Count denote two different things.
int count;  // Different from...
int Count;

No Reserved Keywords:

  • Identifiers and C++ reserved keywords (sometimes called reserved words) cannot be the same. In C++, reserved keywords have set definitions and cannot be changed to become new variable names.
int int;       // Invalid (int is a keyword)
int myVar;     // Valid

Some common keywords you must avoid using as identifiers include if, else, return, class, void, and for.

No Length Limit:

  • In theory, there is no maximum limit to the length of an identifier in C++. However, it is best to use meaningful and concise names for readability.

Examples of Valid and Invalid Identifiers

Here’s a list of valid and invalid identifiers based on the rules we’ve discussed:

Valid IdentifiersInvalid Identifiers
name, studentID2ndStudent (starts with digit)
age, _salary2023salary@home (contains special character)
total_sum, my_value_1my value (contains space)
Name, NAME, nAmEfor (keyword)

Best Practices for Naming Identifiers

Even though you can call your code anything you like in C++ as long as you follow the guidelines, there are some recommended practices that can make your code easier to read and maintain:

  • Use Descriptive Names: Choose identifiers that are self-explanatory and describe the purpose of the variable or function.
int x;               // Avoid this
int studentCount;     // Better
  • Follow Naming Conventions:
    • Variables and Functions: Use camelCase or snake_case for naming variables and functions. CamelCase starts with a lowercase letter and capitalizes subsequent words (e.g., totalMarks, studentID). Snake_case uses underscores to separate words (e.g., total_marks, student_id).
int totalMarks;    // camelCase
int total_marks;   // snake_case
  • Constants: Use all uppercase letters with underscores separating words for constants.
const int MAX_SIZE = 100;
  • Avoid Single-Letter Names (Except in Loops): While short names like i or x are common in loop counters, it’s better to avoid using them for other variables. Descriptive names reduce confusion, especially in large codebases.
  • Keep Identifiers Short but Meaningful: Strive for a balance between brevity and clarity. Names should be neither too long nor too cryptic.
  • Use Consistent Naming Style: Pick a naming convention and stick with it across your code. This consistency makes your code more readable and easier to understand.

Conclusion

In a C++ program, identifiers are essential for classifying and referring to different entities. You can produce clear, intelligible, and maintainable code by abiding by best practices and naming conventions. Make sure your code is as efficient and clear as possible by sticking to naming conventions, avoiding reserved keywords, and always selecting names that make sense.

Learn More:
Comments in C++

Leave a Comment