Identifiers in c programming language
- What are Identifiers in C?
Identifiers are the names given to program elements in C.
They are used to identify variables, functions, arrays, structures, unions, and labels.
👉 Simply: An identifier is a name used to identify something in a program.
- Examples of Identifiers
C
int number;
float total_marks;
void calculateSum();
- Here:
number → variable identifier
total_marks → variable identifier
calculateSum → function identifier
- Rules for Naming Identifiers in C
An identifier must follow these rules:
1.Only alphabets, digits, and underscore (_) are allowed
✅ total_marks
❌ total-marks
2.First character must be a letter or underscore
✅ _count, sum
❌ 1sum
3.Cannot use keywords as identifiers
❌ int, float, return
(These are reserved words)
4.No spaces allowed
❌ total marks
✅ total_marks
5.Case-sensitive
Sum and sum are different identifiers
6.Length
C allows long identifiers, but first 31 characters are significant (for many compilers)
👉Valid and Invalid Identifiers😀
Valid Identifiers
C
marks
_total
student1
sum_of_numbers
- Invalid Identifiers
C
1student // starts with digit
total-marks // special character
float // keyword
total marks // space
- Types of Identifiers in C
1. Variable Identifiers
Used to name variables.
C
int age;
float salary;
2. Function Identifiers
Used to name functions.
C
void display();
int add(int a, int b);
3. Array Identifiers
Used to name arrays.
C
int marks[5];
4. Structure Identifiers
Used to name structures
C
struct Student {
int roll;
char name[20];
};
5. Label Identifiers
Used with goto statement.
C
start:
printf("Hello");
💥 Identifier vs Keyword (Difference)
Identifier Keyword
- User-defined name Pre-defined by C
- Can be changed Cannot be changed
- Example: total Example: int
Comments
Post a Comment