Identifiers and Keywords in C++
In a C++ program (i.e., a program written in C++ language), every word is either classified as an identifier or a keyword.
Identifiers, as the name suggests, are used to identify or name various program elements such as variables, constants, functions etc.
On the other hand, Keywords are a kind of reserved word which have standard, predefined meanings in C++.
Now, let us discuss these concepts in details:
As stated before, identifiers are names that are given to various program elements such as variables, constants, functions, arrays etc. There are certain rules regarding identifier names in C++, as stated below:
- Identifiers must consist of letters and digits, in any order, except that the first character must be a letter
- Both upper-case and lower-case letters are permitted, though common usage favours the use of lower-case letters for most types of identifiers. Upper-case and lower-case letters are not interchangeable i.e., an upper-case letter is not equivalent to the corresponding lower-case letter. Thus the names rate, Rate and RATE denote different identifiers. It is a general practice to use lower-case or mixed case for variable and function names and upper-case for constants
- The underscore character (_) can also be included, and is considered to be a letter. An underscore is often used in the middle of an identifier. An identifier may also begin with an underscore, though this is rarely done in practice
- An identifier should contain enough characters so that its meaning is readily apparent. On the other hand, an excessive number of characters should be avoided
- There are certain reserved words, called keywords, that have standard, predefined meanings in C++. These keywords can be used only for their intended purpose. They cannot be used as a programmer-defined identifiers.
- It is required to note here that the keywords are all lower-case. Since upper-case and lower-case characters are not equivalent, it is possible to utilize an upper-case keyword as an identifier. Normally, however, this is not done, as it is considered a poor programming practice
Examples of valid identifiers:
Examples of Invalid identifiers:
As stated before, keywords are the words whose meaning has already been explained to the C++ compiler (or in a broad sense to the computer). The keywords cannot be used as identifier names because, if we do so, we are trying to assign a new meaning to the keyword, which is not allowed by the computer. The keywords are also called “Reserved words”.
Examples of Keywords in C++: