Chapter 03Beginner

Identifiers in C

Learn the rules for naming things in C - variables, functions, and more. Know what names are valid, and follow naming conventions that professionals use.

10 min readUpdated 2024-12-16
identifiersnaming rulesvariable namescase sensitivenaming conventions

What You Will Learn

  • Understand what identifiers are (names for things)
  • Learn the 5 rules for valid identifier names
  • Know why C is case-sensitive (age ≠ Age)
  • Follow professional naming conventions

01What are Identifiers?

📝 Definition

An identifier is a name you give to things in your program — variables, functions, arrays, structures, etc. It's how you refer to these items in your code.

identifiers_example.c
C
1#include <stdio.h>
2
3int main() {
4 int age = 25; // 'age' is an identifier
5 float salary = 50000.0; // 'salary' is an identifier
6 char grade = 'A'; // 'grade' is an identifier
7
8 printf("Age: %d\n", age);
9 return 0;
10}
11// 'main', 'printf' are also identifiers!

🏷️ Common Uses of Identifiers

Variables

int count, total, sum;

Functions

void calculate(), print();

Arrays

int numbers[10], grades[50];

Structures

struct Student, Person;

02Identifier Naming Rules

📋 The 5 Rules You Must Follow

C has strict rules for naming identifiers. Break any of these, and your code won't compile!

1

Must start with a letter (a-z, A-Z) or underscore (_)

✅ Valid

name, _value, Age

❌ Invalid

1name, 2value, 9Age
2

Can contain letters, digits (0-9), and underscores only

✅ Valid

age1, count_2, num_items

❌ Invalid

my-var, count$, num@2
3

Cannot be a C keyword (reserved word)

✅ Valid

myInt, forLoop, integer

❌ Invalid

int, for, while, return
4

Case-sensitive: uppercase ≠ lowercase

age, Age, and AGE are three different identifiers!

5

No spaces allowed

✅ Valid

firstName, first_name

❌ Invalid

first name, my variable

💡 Length Limit

While C allows very long identifiers, only the first 31 characters are guaranteed to be significant (C89/C90 standard). Modern compilers often support more.

03Valid vs Invalid Examples

IdentifierValid?Reason
studentName✅ YesStarts with letter, only letters
_count✅ YesStarts with underscore
num1✅ YesDigit after first character is OK
MAX_SIZE✅ YesLetters, underscore - all valid
1stPlace❌ NoCannot start with digit
my-var❌ NoHyphen not allowed
float❌ NoReserved keyword
user name❌ NoSpace not allowed
price$❌ NoSpecial characters not allowed
valid_invalid.c
C
1// ✅ VALID Identifiers
2int age;
3float _temperature;
4char firstName;
5int student_count;
6int x1, x2, x3;
7double MAX_VALUE;
8
9// ❌ INVALID Identifiers (will cause errors!)
10// int 2fast; // Cannot start with digit
11// float my-var; // Hyphen not allowed
12// char int; // 'int' is a keyword
13// int user name; // Space not allowed

04Case Sensitivity

⚠️ Important: C is Case-Sensitive!

age, Age, and AGE are THREE DIFFERENT variables!

case_sensitivity.c
C
1#include <stdio.h>
2
3int main() {
4 int age = 25; // lowercase
5 int Age = 30; // capitalized
6 int AGE = 35; // uppercase
7
8 // All three are DIFFERENT variables!
9 printf("age = %d\n", age); // 25
10 printf("Age = %d\n", Age); // 30
11 printf("AGE = %d\n", AGE); // 35
12
13 return 0;
14}
Output

age = 25

Age = 30

AGE = 35

05Naming Conventions

While rules are mandatory, conventions are best practices that make your code readable and professional.

TypeConventionExample
VariablescamelCase or snake_casestudentAge, student_age
ConstantsUPPER_SNAKE_CASEMAX_SIZE, PI_VALUE
FunctionscamelCase or snake_casecalculateSum, calculate_sum
Structs/TypesPascalCaseStudentRecord, Person
MacrosUPPER_SNAKE_CASEMAX_BUFFER, DEBUG_MODE
naming_conventions.c
C
1#include <stdio.h>
2#define MAX_STUDENTS 100 // Constant: UPPER_CASE
3
4struct StudentRecord { // Struct: PascalCase
5 int studentId; // Variable: camelCase
6 char firstName[50];
7 float gradeAverage;
8};
9
10void printStudent(struct StudentRecord s) { // Function: camelCase
11 printf("ID: %d, Name: %s\n", s.studentId, s.firstName);
12}
13
14int main() {
15 int studentCount = 0; // Variable: camelCase
16 return 0;
17}

06Best Practices

✅ Use Meaningful Names

int x; // What is x?

int studentCount; // Clear!

✅ Be Consistent

Pick one style (camelCase or snake_case) and stick with it throughout your project.

✅ Keep It Short but Descriptive

int numberOfStudentsInClass;

int studentCount; // Better

❌ Avoid Starting with Underscore

Names starting with _ or __ are reserved for the system and library functions. Use them only when necessary.

07Common Mistakes

❌ Starting with a Number

int 2ndPlace = 2; // ERROR!

Fix: int secondPlace = 2;

❌ Using Keywords as Names

int return = 5; // ERROR!

Fix: int returnValue = 5;

❌ Case Confusion

int Count = 5; printf("%d", count); // ERROR!

Count ≠ count (case-sensitive)

08Summary

🎯 Key Takeaways

  • Must start with letter or underscore
  • Can contain letters, digits, underscores only
  • No keywords, no spaces, no special characters
  • C is case-sensitive
  • Use meaningful, descriptive names

08Next Steps

Now learn about the 32 reserved keywords in C: