C++ Identifiers
In C++, an identifier is a name given to a variable, function, class, or user-defined item. Identifiers are used to identify these program elements in the code uniquely. Here are some rules and conventions regarding C++ identifiers.
- A C++ identifier is made up of letters, numerals and underscore. letters from A to Z (both uppercase and lowercase), numerals from 0 to 9 and underscore _.
- A C++ identifier may start with underscore or letter, but cannot start with numeral.
- As C++ is case sensitive language. The uppercase and lowercase letters are treated as different letters. So the compiler will treat MyVar as a different identifier from myvar.
- You cannot use keywords as identifiers. However, keyword can be part of an identifier name. For example, an identifier with name int is not allowed as int is keyword, but myint is allowed.
- The ANSI allows 6 significant characters in an external identifier’s name and 31 characters for internal identifiers.
- Identifiers should be meaningful and easy to read. For example, an identifier firstName for ‘First Name’ would be a better choice as compared to x1.
Examples of Valid C++ Identifiers
age, exponent, Temp, WORLD, myint
Examples of Invalid C++ Identifiers
- 2num (starts with numeral)
- float (keyword)
- %name (contains illegal letter %)
- John’s (contains illegal letter ‘)