C++conventions for naming identifiersCPPcpp tutorialsidentifiersidentifiers in cppintroduction of cppinvalid identifiersrule for naming identifiersunderstand basicsvalid identifiers
Identifiers in C++
Identifier
C++ identifiers are names used to identify variable,
function, class or any other user-defined items. For Example Variable name,
Function name, Class name, structure name.
An identifier starts with a letter either capitals (A to Z)
or small (a to z) or an underscore _ that does not begin with a digit. C++ does
not allow us to use punctuation characters such as -, @, $ and % within
identifiers. C++ is a case sensitive programming language. Thus, Demo and demo
are two different identifiers.
Valid Identifiers: Foo, _demo, abc, move_name, a_123, myname50, _temp, a23b9, retVal
Invalid Identifiers: 1Demo, a-123, demo@dss, Amount$100, a%b
Rules for naming an identifier
- Identifier name must begin with a letter, either capital (A to Z) or small (a to z).
- Identifier name does not begin with a digit (0-9).
- It must not contain any space or tab.
- Keywords cannot be used as an identifier.
- Special Symbols are not allowed in identifier names.
- Length of an identifier is at most 32 characters.
- Identifiers are case sensitive.
Let’s discuss some examples
Name
|
Use
|
Explanation
|
1sal
|
No
|
Because identifier does not begin with a digit.
|
first name
|
No
|
Because there should be no space or tab.
|
age@
|
No
|
Because it does not contain special letters.
|
While
|
Yes
|
Because While is not a keyword. (W is capital and small w is for
keyword).
|
_rent
|
Yes
|
Begins with an underscore.
|
for
|
No
|
Because it is a keyword.
|
last_name
|
Yes
|
Because it contains letters and underscore and there is no space
between them.
|
Conventions for naming identifiers
Following conventions is a good practice because it increases
the readability of code. It makes the code easier to understand and thus help
many people understand the code.
- Use only letters either capital or small.
- Use camelCase style for naming an identifier.
- Choose a name which genuinely shows its work.
Thanks for reading. If you like this article then give a Thumbs up. Don't Forget to write your opinion in the comment box and if you think that this article will help any of your friends then do share this article.
0 comments