What are identifiers in C++?

Identifiers are names given to variables which distinguish them from all other variables. The rules of C++ for valid identifiers state that:
An identifier must:

  • start with a letter
  • consist only of letters, the digits 0-9, or the underscore symbol _
  • Not be a reserved word.

Reserved words are otherwise valid identifiers that have special significance to C++. For the purposes of C++ identifiers, the underscore symbol, , is considered to be a letter. Its use as the first character in an identifier is not recommended though, because many library functions in C++ use such identifiers. Similarly, the use of two consecutive underscore symbols, _, is forbidden.

The following are valid identifiers:

Length, days_in_year, DataSet1, Profit95, _Pressure, first_one, first_1 although using _Pressure is not recommended.

The following are invalid:

days-in-year 1data int first.val throw

Identifiers should be chosen to reflect the significance of the variable in the program being written. Although it may be easier to type a program consisting of single character identifiers, modifying or correcting such a program becomes more and more difficult. The minor typing effort of using meaningful identifiers will repay itself many fold in the avoidance of simple programming errors when the program is modified.

Leave a Comment

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