LearnContact
Lesson 87 min read

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.

c
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:

Rule 1: Must Begin with a Letter or Underscore

An identifier can start with an alphabet (A-Z or a-z) or an underscore (_). It cannot begin with a number.

c
// Valid:
age, student, _total

// Invalid:
2age, 9student
Rule 2: Numbers Can Appear Later

Numbers are allowed, but only after the first character.

c
// Valid:
student1, marks2025, item10

// Invalid:
1student
Rule 3: No Spaces Allowed

Identifiers cannot contain spaces. Use camelCase or snake_case instead.

c
// Incorrect:
student name

// Correct:
studentName, student_name
Rule 4: Special Characters Are Not Allowed

Characters like @, #, %, &, *, !, -, +, and / cannot be used.

c
// Incorrect:
student-name, total@marks

// Correct:
studentName, totalMarks
Rule 5: Keywords Cannot Be Used

Identifiers cannot use reserved keywords like int, if, while, or return.

Rule 6: Case Sensitive

C treats uppercase and lowercase letters differently. age, Age, and AGE are three completely different identifiers.

Valid vs Invalid Identifiers

Invalid IdentifierReason
123ageStarts with a number
student-nameContains hyphen
student nameContains space
total@marksContains special character
intKeyword
floatKeyword
#priceStarts 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.

text
studentAge
totalMarks
employeeSalary

Snake Case

Words are separated using underscores.

text
student_age
total_marks
employee_salary

Both styles are widely accepted. The important thing is to remain consistent throughout your program.

Common Beginner Mistakes

Starting with Numbers
// Incorrect:
int 1age = 20;

// Correct:
int age1 = 20;
Using Spaces or Special Characters
// Incorrect:
int student name;
int student-name;

// Correct:
int studentName;
Using Keywords
// Incorrect:
int int = 10;
int while = 5;

// Correct:
int studentCount = 10;
Using Meaningless Names

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.

Next Lesson →

Variables