C++ Variable Naming

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 :

Identifier Note
Name Capital Letter and Small Letters are Allowed
name Small Letters are allowed
name_1 Digits and Underscore is allowed along with alphabets
Int Keywords are allowed but we have to change case of any letter or complete word
INT Keywords are allowed but we have to change case of any letter or complete word
_SUM Underscore at the first position is allowed in C++ language
sum_of_the_numbers We can concatenate multiple words with underscore
firstName Best Style to concatenate multiple words (Changing case of First Letter of Successive Word)
Identifier We can give concept name as Identifier name
printf As we are not going to include stdio.h header file we can use printf as identifier.

Invalid Examples are :

Identifier Explanation
Keyword name cannot be given to Variable/Identifier
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.
$ sign can be used in other programming language for creating identifier, however C/C++ do not support ‘$’ sign.
special characters are not allowed.
num 1
Spaces are not allowed in C++ programming language for declaring identifier.
Digits are allowd but not as first Character