Identifiers in C++ Language Tutorials with Program Example

Identifiers in C++ is a variable, function, class, module, or any other user-defined item object that can be identified by its name in C++, starting with a letter from A to Z, a to Z, or an underscore character is used as the first character of an identifier. One or more letters, digits 0, underscores, or numbers are then added (0 to 9).

The punctuation letters @, $, and percent are not permitted in C++ identifiers. Identifiers are case-sensitive distinctions in a programming language that is important in C++. Consequently, in C++, man and manpower are two separate identifiers.

These are the general guidelines for variable naming:

Identifiers in C++ Language Tutorials
Identifiers in C++ Language Tutorials
  • Numerals, letters, and underscores can all be used in names.
  • To start a name, use a letter or an underscore (_).
  • Case affects names only (myVar and myvar are different variables).
  • Whitespace and special characters like!, #, percent, etc., are not permitted in names.
  • Names cannot contain reserved terms (such as C++ keywords like int).

If we adhere to the guidelines above, we are free to select any name as an identifier. However, we ought to provide an identifier that makes sense with significant names.

Good and Bad Identifier Examples

Invalid IdentifiersBad IdentifierGood Identifier
Total pointsT_pointstotalPoint
1listlist_1list1
floatn_floatfloatNumber

C++ Keywords

The reserved words that have a particular significance to the compiler are known as keywords. They cannot be used as identifiers since they are reserved for a specific application.

For instance, the predefined terms “for,” “break,” “while,” “if,” and “else” are words whose meaning has already been determined by the compiler.

The names a programmer assigns to program elements to identify a variable, functions, arrays, objects, and classes are known as identifiers.

All C++ keywords are listed below.

alignasdecltypenamespacestruct
alignofdefaultnewswitch
anddeletenoexcepttemplate
and_eqdonotthis
asmdoublenot_eqthread_local
autodynamic_castnullptrthrow
bitandelseoperatortrue
bitorenumortry
boolexplicitor_eqtypedef
breakexportprivatetypeid
caseexternprotectedtypename
catchfalsepublicunion
charfloatregisterunsigned
char16_tforreinterpret_castusing
char32_tfriendreturnvirtual
classgotoshortvoid
complifsignedvolatile
constinlinesizeofwchar_t
constexprintstaticwhile
const_castlongstatic_assertxor
continuemutablestatic_castxor_eq

Conclusion

We learned What is an identifier in C++, which can be used as a variable, constant, function name, or user datatype variable, a mix of numbers, letters, and underscores(_), starting with a letter or underscore. All identifiers must go by a few restrictions to be recognized by the compiler; otherwise, an error will be raised. Additionally, an identifier cannot be one of the keywords defined in C++ libraries.

Leave a Comment