Identifiers in C++

Identifiers

A variable in C++ is given a unique name that is known as an identifier.

✏️ Best coding practice: Use descriptive and meaningful names for the variables to make the code self-explanatory.

Rules for naming a variable

The general rules for naming a variable are:

  • An identifier can only contain uppercase alphabets (A to Z), lowercase alphabets (a to z), numbers (0 to 9), and underscore (_).
  • The first letter of an identifier can be an alphabet or an underscore.

✏️ Best coding practice: It is not good practice to start an identifier with an underscore.

  • The first letter of an identifier cannot be a number.
  • C++ is a case-sensitive language. Therefore, an identifier written in the upper case will be different from one written in lower case.

📝Note: numbers and Numbers are two different identifiers.

  • An identifier cannot contain white space.
  • An identifier cannot have special characters such as &, @, *, !, etc.
  • We cannot use keywords as identifiers.

📝Note: Keywords are a collection of reserved words and predefined identifiers in a language used for specific purposes.

Get hands-on with 1200+ tech skills courses.