Identifier


Definition

In C++ (and most programming languages), identifiers are the names used to represent objects, entities, and user-defined elements such as:

  • Variables (e.g., age, studentCount)
  • Functions (e.g., calculateArea(), getName())
  • Classes (e.g., Car, Person)
  • Structs (e.g., struct Point)
  • Enums (e.g., enum Color)
  • Typedefs and Aliases (e.g., typedef unsigned int uint;)
  • Namespaces (e.g., namespace MyNamespace)
  • Objects and Instances (e.g., Car myCar; where myCar is an identifier)

Hence, variable names and function names, for example, all are a subset of identifiers.

In summary, identifiers are the names for objects and entities.


Key Rules / Conventions for Identifiers

✅ Must start with a letter (a-z, A-Z) or an underscore (_).

→ Identifiers beginning with an underscore are typically reserved for system use.

✅ Can contain letters, digits (0-9), and underscores (_).

Cannot contain special characters (e.g., @, #, $) or white spaces (space, tab, new-line).

Case-sensitive (Age and age are different identifiers).

Cannot be a reserved keyword (e.g., int, return, class)


Variable Naming Conventions

  • Since variable names are a subset of identifiers, the key naming conventions listed above extend and also apply to variable naming.
  • Recommended and Best Practices
    • Use meaningful names (e.g., age, numStudents instead of x, a).
    • Follow camelCase: first word in lowercase, subsequent words capitalized (e.g., fontSize, roomNumber).
    • Use self-descriptive names that reflect the variable’s purpose.
    • Avoid single-letter names unless commonly used (e.g., x, y, z for coordinates, i for indexes).
    • It’s okay to use longer names (up to 30+ characters) if they improve clarity.
  • Singular vs Plural Naming
    • Use singular names for individual items (e.g., row).
    • Use plural names for collections (e.g., rows for an array of rows).

Example of Identifiers in C++

int age = 25;         // 'age' is an identifier (also a variable name)
float calculateArea(); // 'calculateArea' is an identifier (also a function name)
class Car {};         // 'Car' is an identifier (also a class name)

Reference

https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp1_Basics.html#zz-3.4

Views: 1


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *