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 :
- It is name given to program element.
- Identifier are the names is given by the programmer.
- An identifier is used for any variable, function, data definition etc.
- We can give any valid name to the identifier.
The rules in C++ for identifiers are :
- Only Alphabets,Digits and Underscores are permitted.
- Identifier name cannot start with a digit.
- Key words cannot be used as a name.
- Upper case and lower case letters are distinct.
- Special Characters are not allowed
- 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 |
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 |