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.
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.
1#include <stdio.h>23int main() {4 int age = 25; // 'age' is an identifier5 float salary = 50000.0; // 'salary' is an identifier6 char grade = 'A'; // 'grade' is an identifier7 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!
Must start with a letter (a-z, A-Z) or underscore (_)
✅ Valid
name, _value, Age❌ Invalid
1name, 2value, 9AgeCan contain letters, digits (0-9), and underscores only
✅ Valid
age1, count_2, num_items❌ Invalid
my-var, count$, num@2Cannot be a C keyword (reserved word)
✅ Valid
myInt, forLoop, integer❌ Invalid
int, for, while, returnCase-sensitive: uppercase ≠ lowercase
age, Age, and AGE are three different identifiers!
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
| Identifier | Valid? | Reason |
|---|---|---|
| studentName | ✅ Yes | Starts with letter, only letters |
| _count | ✅ Yes | Starts with underscore |
| num1 | ✅ Yes | Digit after first character is OK |
| MAX_SIZE | ✅ Yes | Letters, underscore - all valid |
| 1stPlace | ❌ No | Cannot start with digit |
| my-var | ❌ No | Hyphen not allowed |
| float | ❌ No | Reserved keyword |
| user name | ❌ No | Space not allowed |
| price$ | ❌ No | Special characters not allowed |
1// ✅ VALID Identifiers2int age;3float _temperature;4char firstName;5int student_count;6int x1, x2, x3;7double MAX_VALUE;89// ❌ INVALID Identifiers (will cause errors!)10// int 2fast; // Cannot start with digit11// float my-var; // Hyphen not allowed12// char int; // 'int' is a keyword13// int user name; // Space not allowed04Case Sensitivity
⚠️ Important: C is Case-Sensitive!
age, Age, and AGE are THREE DIFFERENT variables!
1#include <stdio.h>23int main() {4 int age = 25; // lowercase5 int Age = 30; // capitalized6 int AGE = 35; // uppercase7 8 // All three are DIFFERENT variables!9 printf("age = %d\n", age); // 2510 printf("Age = %d\n", Age); // 3011 printf("AGE = %d\n", AGE); // 3512 13 return 0;14}age = 25
Age = 30
AGE = 35
05Naming Conventions
While rules are mandatory, conventions are best practices that make your code readable and professional.
| Type | Convention | Example |
|---|---|---|
| Variables | camelCase or snake_case | studentAge, student_age |
| Constants | UPPER_SNAKE_CASE | MAX_SIZE, PI_VALUE |
| Functions | camelCase or snake_case | calculateSum, calculate_sum |
| Structs/Types | PascalCase | StudentRecord, Person |
| Macros | UPPER_SNAKE_CASE | MAX_BUFFER, DEBUG_MODE |
1#include <stdio.h>2#define MAX_STUDENTS 100 // Constant: UPPER_CASE34struct StudentRecord { // Struct: PascalCase5 int studentId; // Variable: camelCase6 char firstName[50];7 float gradeAverage;8};910void printStudent(struct StudentRecord s) { // Function: camelCase11 printf("ID: %d, Name: %s\n", s.studentId, s.firstName);12}1314int main() {15 int studentCount = 0; // Variable: camelCase16 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: