In this guide, you will learn about C identifiers. As the name suggests an identifier in C is a unique name that is used to identify a variable, array, function, structure etc. For example: in int num =10;
declaration, name “num” is an identifier for this int type variable.
Identifier must be unique so that it can identify an entity during the execution of the program.
Rules for C identifiers
As the identifier is chosen by the programmer, there are certain rules in place to remind programmer and to prevent execution of the program until these rules are fulfilled.
1. An identifier can contain lowercase letter, uppercase letters, alphabets, underscore and/or digits. For example, num_2, num8, bigNum etc. are valid identifiers.
2. An identifier cannot start with a digit. For example 99num and 9number are invalid identifiers.
3. An identifier cannot start with an underscore. For example _num, _char are invalid identifiers.
4. An identifier doesn’t allow commas or whitespaces. For example “num 2” and “num, 2” are not allowed in C.
5. The length of an identifier should not exceed 31 characters.
6. The keywords and reserved words cannot be used as an identifiers. For example declaring an variable name with “int” or “for” is not allowed in C as these are the keywords in C.
Although this is not a rule but you should always use meaningful, short and readable identifiers as a good C programming practice.
C identifier Example
#include <stdio.h>
int main()
{
int num=100;
int Num=150;
char ch = 'K';
double bigNumberWithDecimalPoints = 122343434.83823;
printf("Value of num is: %d",num);
printf("\nValue of Num is:%d",Num);
printf("\nValue of ch is: %c",ch);
printf("\nValue of double is: %lf",bigNumberWithDecimalPoints);
return 0;
}
Output: As you can see both the “num” and “Num” identifiers represent different variables with different values so we can say that the identifiers are case sensitive.
Difference between keyword and identifier
Keyword | Identifier |
---|---|
Keywords are pre-defined in the C language which cannot be changed. | Identifier is user defined and can be changed by the programmer. |
Keywords in C are always in lowercase. | Identifiers can be in lowercase or uppercase or both as they are defined by the programmer. |
Keywords are easily identified by the C compiler as their functionalities is already known to the compiler. | Identifier is declared and used by the programmer and their functionality is also defined by the programmer. |
Keywords cannot contain underscore. | Identifier can contain underscore but they cannot begin with underscore. |