C++ Identifiers : Naming Rules of Identifier and Variable Names in C++

What is Identifier ?

Any used defined name given to the program element is called as identifier. (i.e Program elements are identified by program with the identifier name)

Some Facts About Identifier :

  1. It is name given to program element.
  2. Identifier are the names is given by the programmer.
  3. An identifier is used for any variable, function, data definition etc.
  4. We can give any valid name to the identifier.

The rules in C++ for identifiers are :

  1. Only Alphabets,Digits and Underscores are permitted.
  2. Identifier name cannot start with a digit.
  3. Key words cannot be used as a name.
  4. Upper case and lower case letters are distinct.
  5. Special Characters are not allowed
  6. Global Identifier cannot be used as “Identifier”.

Sample Examples of C++ Identifier :

In the C++ programming language, an identifier is a combination of alphanumeric characters, the first being a letter of the alphabet or an underline, and the remaining being any letter of the alphabet, any numeric digit, or the underline.

Valid Examples are :

IdentifierNote
NameCapital Letter and Small Letters are Allowed
nameSmall Letters are allowed
name_1Digits and Underscore is allowed along with alphabets
IntKeywords are allowed but we have to change case of any letter or complete word
INTKeywords are allowed but we have to change case of any letter or complete word
_SUMUnderscore at the first position is allowed in C++ language
sum_of_the_numbersWe can concatenate multiple words with underscore
firstNameBest Style to concatenate multiple words (Changing case of First Letter of Successive Word)
IdentifierWe can give concept name as Identifier name
printfAs we are not going to include stdio.h header file we can use printf as identifier.

Invalid Examples are :

IdentifierExplanation
int
Keyword name cannot be given to Variable/Identifier
pow
pow() is defined in math.h. This variable is legal if we haven’t included math.h in our program. As soon as we include math.h header file in program this identifier will be illegal.
$sum
$ sign can be used in other programming language for creating identifier, however C/C++ do not support ‘$’ sign.
num^2
special characters are not allowed.
num 1
Spaces are not allowed in C++ programming language for declaring identifier.
2num
Digits are allowd but not as first Character