Last Updated on August 18, 2023 by Mayank Dham
Within this article, we shall delve into the realm of keywords and identifiers in C language. To put it plainly, keywords represent distinctive, predefined terms with meanings already recognized by the compiler. On the other hand, identifiers play a pivotal role in labeling variables, functions, and arrays. They serve as the means to distinctly designate a particular variable’s name.
What are Keywords in C
These words have special meanings and we cannot use these words elsewhere in the program This word is used in a specific condition and it specifies the type of some particular variable? No special symbol can be used keywords have standard predefined meanings. Keywords are also known as reserved words and we cannot alter the meaning of keywords. Keywords are the basic building blocks for developing C programs.
All Keywords in C
There are only 32 keywords in standard C and these keywords must be written in lowercase.
All keywords in c are following
auto break case char const continue
default do double else enum extern
float for goto if int long
register return short signed static
struct switch typedef union unsigned void
volatile while
Identifiers
These are user-defined names used for naming of variables, function and arrays
We cannot use keyword as identifiers. Identifiers must be different from the keywords because we have studied earlier that keywords are reserved words
That have predefined meanings. Identifiers are simple text or strings to identify various identities. An identifier can consist of alphabetical digits characters and underscores. The first character can be an uppercase, lowercase letter, or underscore.
Rules for Naming Identifiers in C
- Identifier should not be a keyword
- Identifiers can not start with a digit.
- All the identifiers should have a unique name in the same scope
- The first character of an identifier should always start with an alphabet or underscore, and then it can be followed by any of the characters, digit, or underscore.
- The special characters such as ‘*’,’#’,’@’,’$’ are not allowed within an identifier.
- All the identifiers are case sensitive means the identifiers "hello" and "Hello" will be treated differently. However, both names are identical, but one has a lowercase alphabet, and the other has an uppercase alphabet.
- Length of an identifier should not exceed 31 characters.
- Any blank spaces or commas are not allowed within an identifier.
Types of Identifiers in C
There are two types of identifiers in C language
1. Internal Identifier
These are also known as internal names which includes the names of local variables. Internal
Identifier can be a local variable that has 31 significant characters.
2. External Identifier
These identifiers are used in the external link process (process refers to a whole program that combines all the translation units(implemented file + all header files ). External identifiers are also known as external names .it can be a name of a function or a global variable. it has at least 63 significant characters.
Difference between Keywords and Identifiers
Keyword | Identifiers |
---|---|
These are special specific, reserved words that have special meanings and we cannot use these words elsewhere in the program | Identifiers are simple text or strings to identify various identities. An identifier can consist of alphabetical digits characters and underscores. |
The keyword specifies the variable type | Identifier specifies the name of the variable |
No special symbol can be used because these are not user-defined. | A special symbol Underscore can be used |
Keyword in c always consists of lowercase alphabets and starts with lower case | Identifier can be both uppercase and lowercase, and it can start with the alphabet or underscore. |
Example-:for, float, int, while, do, class, etc. | Example -: Vikas, name,school_no, etc. |
Code Implementation
#include <stdio.h> // creating a function int add_one(int number) { return number+1; } int main() { // creating some data variables char Ch = 'c'; int a; double double_number = 3.14; int z = add_one(5); int arr[5]; // array of size five return 0; }
Result:
Identifier- {Ch,double_number,z, arr,a}
Keyword -{int, double, char, return }
Explanation:
In the above C program we first make a function with the name add_one, here add_one is an identifier,add_one function takes a number in an argument and then simply add 1 to that number. So here add_one is (text with the special character) or a name of the function and this function has return type int. Here it is a keyword that cannot be changed if the return type of the function is integer type you have to use only int. In the main function, we have created three different types of variables with identifiers Ch, a,double_number, and z. In the end, we have created an array with the identifier arr.
Valid Identifiers
- Size It contains only lowercase alphabets.
- Name_school It contains only ‘_’ as a special character.
- Roll1 Here, the numeric digit comes at the end.
- roll_2 It starts with lowercase and ends with a digit.
- _size Contains lowercase alphabets and an underscore and It starts with an underscore ‘_’
Invalid Identifiers
- Float it is a keyword
- 1name It begins with digit
- \$god It starts with special character
- x y it contains a blank space
- x/y it contains special character
Conclusion
Keywords and identifiers are fundamental concepts in the C programming language that play a crucial role in writing and understanding code. Keywords are reserved words that have specific meanings and purposes within the language, while identifiers are names given to various program entities, such as variables, functions, and labels. Proper understanding and usage of keywords and identifiers are essential for writing clean, readable, and error-free C code.
FAQs related to Keywords & Identifiers in C Language
1. How many keywords are there in C?
The C language has a set of around 32 keywords (the exact number may vary slightly between different compilers), such as int, if, else, for, while, return, switch, and typedef, among others.
2. What are identifiers in C?
Identifiers in C are names given to various program entities, such as variables, functions, arrays, and labels. They are user-defined and provide a way to reference and manipulate different elements within a program.
3. What are the rules for naming identifiers in C?
Identifiers in C must follow certain rules:
Must start with a letter (uppercase or lowercase) or an underscore.
After the initial character, can consist of letters, digits, and underscores.
Case-sensitive: myVar and MyVar are treated as different identifiers.