Every language has keywords and identifiers, which are only understood by its compiler. Keywords are predefined reserved words, which possess special meaning. Each keyword defines the “type” declared data. Keywords should not be used as identifiers. An identifier is a unique name given to a particular variable, function or label of class in the program. To create a variable, both a keyword and an identifier are bind together.
The identifiers can be altered if required, while this is not the case with keywords, which are fixed, we can not change it according to our need. This content further elaborates the difference between a keyword and an identifier.
Content: Keyword Vs Identifier
Comparison chart
Basis for Comparison | Keyword | Identifier |
---|---|---|
Basic | Keywords are the reserved words of a language. | Identifiers are the user defined names of variable, function and labels. |
Use | Specify the type/kind of entity. | Identify the name of a particular entity. |
Format | Consider only letters. | Consider letters, underscore, digits. |
Case | Use only lowercase. | Lower and upper cases, both are allowed. |
Symbol | No special symbol, punctuation is used. | No punctuation or special symbol except 'underscore' is used. |
Classification | Keywords are not further classified. | Identifier are classified into 'external name' and 'internal name'. |
Starting letter | It always starts with a lowercase letter. | First character can be a uppercase, lowercase letter or underscore. |
Example | int, char, if, while, do, class etc. | Test, count1, high_speed, etc. |
Definition of Keywords
The words which are reserved by C++ are called “keywords”. These keywords can not be used to name an identifier and to name other entity of the program. Each keyword has a distinct meaning and is used by a compiler to perform a specific action. For example, ‘int’ is used to create an identifier of integer type, ‘float’ is used to create an identifier of float type.
Example
To get a clear image of keywords, let’s overview a real-life example. We have a ‘book’ named ‘Complete Reference’. Here the word ‘Book’ is the keyword, and the name “complete_reference” is an identifier to the keyword ‘Book’. Now, Keyword specifies, what is a “complete_reference”, the answer is, it is a “Book”.
Now, let’s take an example of a program if we write ‘float salary’. Here, ‘keyword’ is ‘float’ and ‘salary’ is an ‘identifier’. Now, if you ask what do you mean by ‘salary’, the answer is, it specifies that it is a ‘variable’ which is ‘float’ in nature and accepts ‘float values’.
Definition of Identifier
The name you provide to an entity in a program so that it can be uniquely identified is called “identifier”. The names of variables, functions, labels of a class and various other user-defined entities are ‘identifiers’. The identifier can never be used as a ‘keyword’.
Example
To understand it in a better way, let’s elaborate the above example of ‘keyword’. As “Book” is a ‘keyword’ and “Complete_reference” is an identifier. Now, if we want a ‘Book of Complete Reference’. When we ask to the shopkeeper, that we want a “Book” he/She will not identify which ‘Book’, unless we specify the books name, i.e. “complete_reference”.
Now, take the above example of a program, we know ‘float’ is a ‘keyword’, and ‘salary’ is an ‘identifier’. Now, if you want to print the value of variable ‘salary’, you have to explicitly call the variable’s name to print the ‘salary’, here, calling ‘float’ won’t work. So identifier is a name by which we can call our created entity in a program.
Rules for constructing an identifier
- The first character of an identifier should mandatorily be a letter. (‘_’ Underscore can also be used as the first letter)
- It can be a set of letters, digits and underscore.
- Uppercase and lowercase letters are treated differently.
- All of the characters are significant.
- The use of reserved words as the name of identifiers or variables are forbidden.
- White spaces are not allowed.
- An identifier can contain 1024 characters at maximum, as ANSI standard requires that the C++ compilers must provide at least this number of characters.
Key Differences Between Keyword and Identifier
- Keywords are used to recognize the type/kind of entity while an identifier is used to name that entity uniquely. For example, if we write ‘int number’, where ‘int’ is a keyword, and ‘number’ is an identifier, i.e., this statement clearly defines that we define an entity ‘number’ which is of type int(integer).
- Keywords are distinct; they are not further classified. On the contrary, if identifiers are involved in an external link process, i.e. if it include function names and global variable that are shared between files, then it is called ‘external names’, while they are not used in external link process and include the name of local variable, then it is called ‘internal names’.
- The identifier can never be same as keywords, and the name of functions that are in C++ library.
- Keywords defined in C++ library does not contain any symbol. Conversely, when you declare any identifier you can only use underscore but not any other symbol.
- A keyword always starts with a lower case. As against, an identifier can either start with an upper case or a lower case
Conclusion
Keywords and identifiers are the building blocks of the program. They are specifically used by a compiler to uniquely define the type/kind and a name of a particular variable or a function of a class.
Best SMS Company says
Hey, thank you for providing us such a good explanation to us.
Dhruv says
Good information
Jyoti says
Informative content……
Payal londhe says
What difference between variable, identifier and keywords?
Bisman kaur says
Thanks a lot for this information. It helped me a lot.