Identifiers & Naming Rules
Identifiers are user-defined names given to program elements. In this lesson, you will learn the 6 strict rules for naming identifiers, naming conventions, and how to choose meaningful names.
Introduction
When writing a program, we need names to identify different elements such as variables, functions, arrays, structures, and labels. These names help both the programmer and the compiler distinguish one element from another. In the C programming language, these names are called Identifiers. Choosing meaningful identifiers makes programs easier to read, understand, and maintain.
What is an Identifier?
An Identifier is a user-defined name given to program elements such as variables, functions, arrays, structures, unions, and labels. The compiler uses these names to identify and access different components of the program. Unlike keywords, identifiers are created by the programmer.
Real-World Analogy
Imagine a classroom with many students. Instead of calling everyone "Student," each student has a unique name like Rahul, Priya, Amit, or Neha. These names help the teacher identify each student individually. Similarly, in C programming, variables, functions, and arrays have unique names called Identifiers.
In a Classroom
- Rahul (Unique name)
- Priya (Unique name)
- Amit (Unique name)
In C Programming
- age (Variable name)
- salary (Variable name)
- calculateSum (Function name)
Why Do We Need Identifiers?
Identifiers help programmers identify variables, call functions, access arrays, use structures, improve program readability, and organize code efficiently. Without identifiers, programming would be almost impossible.
Where are Identifiers Used?
Identifiers can be used for naming variables, functions, arrays, structures, unions, enumerations, labels, and user-defined data types.
int age; // 'age' is an identifier
float salary; // 'salary' is an identifier
void displayMessage() // 'displayMessage' is an identifier
{
}6 Rules for Naming Identifiers
Identifiers must follow certain strict rules defined by the C language:
An identifier can start with an alphabet (A-Z or a-z) or an underscore (_). It cannot begin with a number.
// Valid:
age, student, _total
// Invalid:
2age, 9studentNumbers are allowed, but only after the first character.
// Valid:
student1, marks2025, item10
// Invalid:
1studentIdentifiers cannot contain spaces. Use camelCase or snake_case instead.
// Incorrect:
student name
// Correct:
studentName, student_nameCharacters like @, #, %, &, *, !, -, +, and / cannot be used.
// Incorrect:
student-name, total@marks
// Correct:
studentName, totalMarksIdentifiers cannot use reserved keywords like int, if, while, or return.
C treats uppercase and lowercase letters differently. age, Age, and AGE are three completely different identifiers.
Valid vs Invalid Identifiers
| Invalid Identifier | Reason |
|---|---|
| 123age | Starts with a number |
| student-name | Contains hyphen |
| student name | Contains space |
| total@marks | Contains special character |
| int | Keyword |
| float | Keyword |
| #price | Starts with special character |
Identifier Length & Types
There is no fixed length for identifiers in modern C compilers. However, short names are easier to type, while meaningful names improve readability. Instead of writing x, write studentAge.
Types of Identifiers
Variable
- age
- salary
- marks
Function
- calculateArea
- displayResult
- findMaximum
Array
- numbers
- students
- marks
Structure
- Student
- Employee
- Product
Enumeration
- Color
- WeekDay
- Direction
Naming Conventions
Although C does not force a naming style, programmers commonly follow these conventions to keep code clean:
Camel Case
The first word starts with a lowercase letter. Each following word begins with an uppercase letter.
studentAge
totalMarks
employeeSalarySnake Case
Words are separated using underscores.
student_age
total_marks
employee_salaryBoth styles are widely accepted. The important thing is to remain consistent throughout your program.
Common Beginner Mistakes
// Incorrect:
int 1age = 20;
// Correct:
int age1 = 20;// Incorrect:
int student name;
int student-name;
// Correct:
int studentName;// Incorrect:
int int = 10;
int while = 5;
// Correct:
int studentCount = 10;Poor: a, x, temp1. Better: employeeName, totalAmount, averageMarks.
Best Practices
- Choose meaningful names that describe the data.
- Follow a consistent naming style. Stick to either camelCase or snake_case.
- Avoid unnecessary abbreviations. Use studentAge instead of stdAge.
- Keep identifiers simple and readable.
- Never use keywords as identifiers.
Frequently Asked Questions
What is an identifier?
An identifier is a user-defined name used to identify variables, functions, arrays, structures, and other program elements.
Can an identifier start with a number?
No. An identifier must begin with a letter or an underscore.
Can identifiers contain spaces?
No. Spaces are not allowed in identifiers.
Are identifiers case-sensitive?
Yes. age, Age, and AGE are treated as three different identifiers.
Can I use keywords as identifiers?
No. Keywords are reserved words and cannot be used as identifiers.
Key Takeaways
- Identifiers are user-defined names used to identify program elements.
- An identifier must begin with a letter or underscore.
- Numbers can appear after the first character.
- Spaces and special characters are not allowed.
- Keywords cannot be used as identifiers.
- Meaningful identifiers improve code readability and maintainability.
Summary
Identifiers are one of the most fundamental concepts in C programming because they provide names to the various elements of a program. Following proper naming rules and conventions not only helps the compiler recognize program elements but also makes the code easier for humans to read and understand. Well-chosen identifiers are an essential part of writing clean, professional-quality programs.