Identifiers in C language are names used to identify variables, functions, arrays, structures, or any other user-defined items. They serve as labels for memory locations in the program, allowing you to reference and manipulate data efficiently. Identifiers are crucial for writing readable and organized code.
Rules for Naming Identifiers
Identifiers in C must adhere to specific rules:
Allowed Characters:
- Identifiers can contain letters (both uppercase and lowercase), digits (0-9), and underscore (
_
). - They must start with a letter or underscore. Starting with a digit is not allowed.
- Example:
age
,_value
,count2
.
- Identifiers can contain letters (both uppercase and lowercase), digits (0-9), and underscore (
Case Sensitivity:
- Identifiers are case-sensitive. For example,
Total
andtotal
are considered different identifiers.
- Identifiers are case-sensitive. For example,
Cannot Use Reserved Keywords:
- Identifiers cannot be keywords such as
int
,return
,for
, etc., because these are reserved for language-specific purposes.
- Identifiers cannot be keywords such as
No Special Characters:
- Identifiers cannot contain special characters like
@
,#
,$
,-
, etc., except for the underscore (_
).
- Identifiers cannot contain special characters like
Length:
- The length of an identifier can vary based on the compiler, but it is typically advisable to keep them within a reasonable length for readability.
Examples of Valid and Invalid Identifiers
- Valid Identifiers:
age
,_value
,count2
,TotalValue
,student_marks
. - Invalid Identifiers:
2count
(starts with a digit),total-value
(contains a hyphen),float
(keyword),#name
(contains special character#
).
Examples of Identifiers in Use
Here are some examples of identifiers in a simple C program:
#include <stdio.h>
int main() {
int age = 25; // 'age' is an identifier for the integer variable
float height = 5.9; // 'height' is an identifier for the float variable
char initial = 'A'; // 'initial' is an identifier for the character variable
printf("Age: %d\n", age); // 'printf' is a library function identifier
printf("Height: %.1f\n", height);
printf("Initial: %c\n", initial);
return 0; // 'main' and 'return' are also identifiers
}
Types of Identifiers
- Variables: These are memory locations where data can be stored.
int number; // 'number' is an identifier for an integer variable.
- Functions: Functions are also identified using names that act as identifiers.
int add(int a, int b) { return a + b; // 'add' is an identifier for the function name. }
- Arrays: Arrays are collections of similar data types.
int scores[5]; // 'scores' is an identifier for an array.
- Structures and Enums: Custom data types defined by users are given names, which are identifiers.
struct Student { char name[50]; int age; }; // 'Student' is an identifier for the structure.
Best Practices for Choosing Identifiers
- Use Meaningful Names: Choose names that convey the purpose of the variable or function. For example, use
totalSum
instead ofts
. - CamelCase or Snake_case: Use consistent naming conventions like
totalSum
(camelCase) ortotal_sum
(snake_case). - Avoid Very Short Names: Short names like
x
ory
can be used in loops but should be avoided for identifiers that represent key parts of the program. - Avoid Starting with an Underscore: Though allowed, identifiers starting with an underscore are conventionally reserved for system-level or internal code, especially in the global namespace.
Summary
- Identifiers are names for variables, functions, arrays, etc.
- They must start with a letter or underscore and can contain letters, digits, and underscores.
- Keywords and special characters are not allowed in identifiers.
- Use identifiers that are meaningful and descriptive to make the code easier to understand.
Using good identifier names helps in making code readable, understandable, and maintainable.