Identifiers in C++ Programming

Definition of Identifiers

An identifier is a programmer-defined name that represents some element of a program. Variable names are examples of identifiers. You may choose your own variable names in C++, as long as you do not use any of the C++ key words.

C++ Key Words
and continue goto public try
and_eq default if register typedef
asm delete inline reinterpret_cast Typeid
auto do int return typename
bitand double long short union
bitor dynamic_cast mutable signed unsigned
bool else namespace sizeof using
break enum new static virtual
case explicit not static_cast void
catch export not_eq struct volatile
char extern operator switch wchar_t
class false or template while
compl float or_eq this xor
const for private throw xor_eq
Const_cast friend protected true

Legal Identifiers

Regardless of which style you adopt, be consistent and make your variable names as sensible as possible. Here are some specific rules that must be followed with all C++ identifiers.

  • The first character must be one of the letters a through z, A through Z, or an underscore character (_).
  • After the first character you may use the letters a through z or A through Z, the digits 0 through 9, or underscores.
  • Uppercase and lowercase characters are distinct. This means ItemsOrdered is not the same as itemsordered.
Variable Name Legal or Illegal
dayOfWeek Legal.
3dGraph Illegal. Variable names cannot begin with a digit.
_employee_num Legal.
June1997 Legal.
Mixture#3 Illegal. Variable names may only use letters, digits, and underscores.

Ads Right