In C++, every variable, function, and object needs a unique name—these are called identifiers. They help programmers reference and manipulate stored values efficiently.
Choosing Identifiers
Identifiers can be short (x
, y
) or descriptive (engineCapacity
, totalMileage
). Using clear and meaningful names improves code readability.
Example of Good vs. Vague Naming:
C++
x
// Good: Clearly describes the stored value
int fuelPerTank = 15;
// Acceptable but unclear: Hard to understand without context
int f = 15;
Rules for Naming Identifiers
When creating identifiers, follow these guidelines:
- Allowed: Letters (
A-Z, a-z
), digits (0-9
), and underscores (_
). - Must start with a letter or an underscore (
_
). - Case-sensitive:
enginePower
andEnginePower
are different identifiers. - Cannot contain spaces or special characters (
!
,#
,%
, etc.). - Reserved words (such as
int
,double
,return
) cannot be used as identifiers.