Identifiers in C
In C, Identifiers are the names used to identify variables, functions, arrays, structures, or other elements in a program. They help the compiler and programmer to uniquely recognize different entities in the code.
Identifiers are chosen by the programmer, unlike keywords which are reserved by the language. However, there are certain rules and conventions that must be followed while creating identifiers.
Rules for Identifiers
Important Rules:
- Identifiers must begin with a letter (A–Z or a–z) or underscore (_).
- They can contain letters, digits, and underscore, but cannot start with a digit.
- Identifiers are case-sensitive (
Ageandageare different). - They cannot be a keyword (e.g.,
int,while). - Special characters like
@, #, $, %are not allowed. - There is no fixed length limit, but very long names are not recommended.
Detailed Explanation of Rules
1. Identifiers must begin with a letter or underscore (_)
Every identifier in C should start with an alphabet (A–Z or a–z) or an underscore. This helps the compiler correctly recognize the identifier.
// Valid
int age;
int _number;
// Invalid
int 1value; // cannot start with digit
2. They can contain letters, digits, and underscore (but not start with digit)
After the first character, an identifier may contain alphabets, numbers, or underscores. Digits are allowed only after the first character.
// Valid
int marks1;
float student_2;
// Invalid
int 9marks; // starts with digit
3. Identifiers are case-sensitive
C treats uppercase and lowercase letters differently.
Therefore, Age and age are considered different identifiers.
int Age = 20;
int age = 15;
// Both variables are different.
4. Identifiers cannot be a keyword
Keywords are reserved words in C and cannot be used as identifiers. Using them will generate a compilation error.
// Invalid
int int = 10;
float while = 3;
5. Special characters are not allowed
Identifiers can contain only alphabets, digits, and underscores.
Symbols like @, #, $, % are not valid.
// Invalid
int total$marks;
float #price;
6. No fixed length limit (but avoid very long names)
C allows long identifiers, but meaningful and short names are preferred for better readability.
// Valid (but not recommended)
int this_is_a_very_long_variable_name = 100;
// Better
int totalMarks = 100;
Best Practices for Identifiers
-
Use meaningful names (e.g.,
studentAgeinstead ofsa). - Keep names short but descriptive.
- Avoid unnecessary special patterns or confusing names.
- Use consistent naming style in the program.
Real-Life Analogy:
Think of identifiers as names of students in a classroom. Each student must have a unique name or roll number for identification. Similarly, identifiers give unique names to variables and functions so the compiler can recognize them.
Example Program Using Identifiers
// C program using identifiers
#include <stdio.h>
int main() {
int marks = 90;
float percentage = 90.5;
printf("Marks: %d\n", marks);
printf("Percentage: %.1f", percentage);
return 0;
}
Output:
Marks: 90
Percentage: 90.5
Explanation:
- marks and percentage are identifiers.
- These identifiers are used to store values in the program.
- printf() is a predefined function used to display output and it is an identifier.
- Both identifiers follow naming rules because they start with letters and contain no special characters.
Valid vs Invalid Identifiers
| Valid Identifiers | Invalid Identifiers |
|---|---|
| totalMarks | 2value |
| _counter | float (keyword) |
| roll_no | student-name |
| Value123 | price$ |
Summary:
- Identifiers are names given to variables, functions, arrays, etc.
- They must follow identifier naming rules.
- Identifiers cannot start with digits or contain special characters.
- Identifiers are case-sensitive.
- Examples of valid identifiers:
age,studentName,_total. - Examples of invalid identifiers:
1value,while,my-name.