Identifiers in C Language
Identifiers in C Language serve as names for various elements within a program, such as variables, functions, arra
ys, structures, unions, labels, and more. An identifier can consist of letters, both uppercase and lowercase, digits, and underscores, but it must start with either an alphabet letter or an underscore. If an identifier is exclusively used within the program it’s defined in, it’s called an internal identifier. On the other hand, if an identifier is used across multiple program files, it’s referred to as an external identifier. C Language Identifiers are essential for naming variables, functions, and other elements within C programs.In essence, an identifier is a sequence of alphanumeric characters that must commence with an alphabetical character or an underscore. These identifiers are essential for representing a variety of programming constructs. There are a total of 52 alphabetical characters (including both uppercase and lowercase letters), the underscore character, and ten numerical digits (0-9) that can be used in identifiers, amounting to a total of 63 alphanumeric characters for identifier creation.
Rules for constructing C identifiers:
- The first character of an identifier must be an alphabetical character or an underscore, followed by any combination of characters, digits, or underscores.
- Identifiers cannot begin with a numerical digit.
- C identifiers are case-sensitive, distinguishing between uppercase and lowercase letters.
- Commas or blank spaces cannot be included within an identifier.
- Keywords reserved by the C language cannot be used as identifiers.
- Identifiers should not exceed 31 characters in length.
- It’s advisable to create identifiers that are meaningful, concise, and easy to understand.
Examples of valid identifiers:
total, sum, average, _m _, sum_1, etc.
Example of invalid identifiers:
2sum (starts with a numerical digit)
int (reserved word)
char (reserved word)
m+n (special character, i.e., '+')
Types of identifiers:
- Internal Identifier: If an identifier is only used within the program where it’s defined, it’s known as an internal identifier. Internal identifiers are typically associated with local variables.
- External Identifier: When an identifier is used across multiple program files, it’s referred to as an external identifier. External identifiers are often associated with function names and global variables.
Differences between Keyword and Identifier in C Language:
Keyword | Identifier |
---|---|
Keyword is a pre-defined word. | The identifier is a user-defined word. |
It must be written in a lowercase letter. | It can be written in both lowercase and uppercase letters. |
Its meaning is pre-defined in the c compiler. | Its meaning is not defined in the c compiler. |
It is a combination of alphabetical characters. | It is a combination of alphanumeric characters. |
It does not contain the underscore character. | It can contain the underscore character. |
Let’s illustrate this with an example:
int main()
{
int a = 10;
int A = 20;
printf("Value of a is: %d", a);
printf("\nValue of A is: %d", A);
return 0;
}
Output:
Value of a is: 10
Value of A is: 20
The output demonstrates that ‘a’ and ‘A’ have different values, highlighting the case sensitivity of identifiers.”
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.