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. This content further elaborates the difference between keyword and an identifier.
Content: Keyword Vs Identifier
|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. A keyword cannot be used 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.A keyword can not be used to name an identifier. A keyword cannot be used to name other entity of the program.
To get a clear image of keywords lets 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, 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 objects are ‘identifiers’. The identifier can never be used as a ‘keyword’.
To understand it in a better way, let’s elaborate the above example of ‘keyword’ . As “Book” is a ‘keyword’ and “Complete_refernce” is an identifier. Now, if we want a ‘Book of Complete Reference’, and to the shopkeeper, we only ask for the “Book” it will not identify which ‘Book’, unless we specify the books name i.e. “complete_reference”.
Now, take 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. In C++, there is no limit on the length of an identifier.
Key Differences Between Keyword and Identifier
- Keywords are used to recognize the type/kind of entity and identifier is used to uniquely name that entity. 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. Whereas, 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 whereas, when you declare any identifier you can only use underscore but not any other symbol.
- A keyword always starts with a lower case, but, an identifier can either start with an upper case or a lower case
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.