Identifiers In Cpp


While mathematicians are content with giving their variables one-letter names like x, programmers should use longer, more descriptive variable names. Names such as altitude, sum, and user_name are much better than the equally permissible a, s, and u. A variable’s name should be related to its purpose within the program. Good variable names make programs more readable by humans. Since programs often contain many variables, well-chosen variable names can render an otherwise obscure collection of symbols more understandable.

C++ has strict rules for variable names. A variable name is one example of an identifier. An identifier is a word used to name things. One of the things an identifier can name is a variable. We will see in later chapters that identifiers name other things such as functions and classes. Identifiers have the following form:

Identifiers must contain at least one character.

  • The first character must be an alphabetic letter (upper or lower case) or the underscore ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_
  • The remaining characters (if any) may be alphabetic characters (upper or lower case), the underscore, or a digit ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789
  • No other characters (including spaces) are permitted in identifiers.

A reserved word cannot be used as an identifier(see below table)

Here are some examples of valid and invalid identifiers:

  • All of the following words are valid identifiers and so qualify as variable names: x, x2, total, port_22, and FLAG.
  • None of the following words are valid identifiers: sub-total (dash is not a legal symbol in an identifier), first entry (space is not a legal symbol in an identifier), 4all (begins with a digit), #2 (pound sign is not a legal symbol in an identifier), and class (class is a reserved word). C++ reserves a number of words for special use that could otherwise be used as identifiers. Called reserved words or keywords, these words are special and are used to define the structure of C++ programs and statements.
  • Below table lists all the C++ reserved words.
  • The purposes of many of these reserved words are revealed throughout this tutorial. You may not use any of the reserved words in below table as identifiers. Fortunately, if you accidentally attempt to use one of the reserved words in a program as a variable name, the compiler will issue an error.
  • Some programming languages do not require programmers to declare variables before they are used; the type of a variable is determined by how the variable is used. Some languages allow the same variable
asm else new this
auto enum operator throw
bool explicit private true
break export protected try
case extern public typedef
catch false register typeid
char float reinterpret_cast typename
class for return union
const friend short unsigned
const_cast goto signed using
continue if sizeof virtual
default inline static void
delete int static_cast volatile
do long struct wchar_t
double mutable switch while
dynamic_cast namespace template

Trigraphs :

A few characters have an alternative representation, called a trigraph sequence. A trigraph is a three-character sequence that represents a single character and the sequence always starts with two question marks.

Trigraphs are expanded anywhere they appear, including within string literals and character literals, in comments, and in preprocessor directives.

Following are most frequently used trigraph sequences −

Trigraph Replacement
??= #
??/ \
??' ^
??( [
??) ]
??! |
??< {
??> }
??- ~
#identifiers_in_c++ #keywords_in_c++ #Trigraphs_in_c++

(New page will open, for Comment)

Not yet commented...