Identifiers in C++ is a variable, function, class, module, or any other user-defined item object that can be identified by its name in C++, starting with a letter from A to Z, a to Z, or an underscore character is used as the first character of an identifier. One or more letters, digits 0, underscores, or numbers are then added (0 to 9).
The punctuation letters @, $, and percent are not permitted in C++ identifiers. Identifiers are case-sensitive distinctions in a programming language that is important in C++. Consequently, in C++, man and manpower are two separate identifiers.
These are the general guidelines for variable naming:
- Numerals, letters, and underscores can all be used in names.
- To start a name, use a letter or an underscore (_).
- Case affects names only (myVar and myvar are different variables).
- Whitespace and special characters like!, #, percent, etc., are not permitted in names.
- Names cannot contain reserved terms (such as C++ keywords like int).
If we adhere to the guidelines above, we are free to select any name as an identifier. However, we ought to provide an identifier that makes sense with significant names.
Good and Bad Identifier Examples
Invalid Identifiers | Bad Identifier | Good Identifier |
---|---|---|
Total points | T_points | totalPoint |
1list | list_1 | list1 |
float | n_float | floatNumber |
C++ Keywords
The reserved words that have a particular significance to the compiler are known as keywords. They cannot be used as identifiers since they are reserved for a specific application.
For instance, the predefined terms “for,” “break,” “while,” “if,” and “else” are words whose meaning has already been determined by the compiler.
The names a programmer assigns to program elements to identify a variable, functions, arrays, objects, and classes are known as identifiers.
All C++ keywords are listed below.
alignas | decltype | namespace | struct |
alignof | default | new | switch |
and | delete | noexcept | template |
and_eq | do | not | this |
asm | double | not_eq | thread_local |
auto | dynamic_cast | nullptr | throw |
bitand | else | operator | true |
bitor | enum | or | try |
bool | explicit | or_eq | typedef |
break | export | private | typeid |
case | extern | protected | typename |
catch | false | public | union |
char | float | register | unsigned |
char16_t | for | reinterpret_cast | using |
char32_t | friend | return | virtual |
class | goto | short | void |
compl | if | signed | volatile |
const | inline | sizeof | wchar_t |
constexpr | int | static | while |
const_cast | long | static_assert | xor |
continue | mutable | static_cast | xor_eq |
Related Articles
Conclusion
We learned What is an identifier in C++, which can be used as a variable, constant, function name, or user datatype variable, a mix of numbers, letters, and underscores(_), starting with a letter or underscore. All identifiers must go by a few restrictions to be recognized by the compiler; otherwise, an error will be raised. Additionally, an identifier cannot be one of the keywords defined in C++ libraries.