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.
Track Your Progress
Sign in to save your learning progress
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
01Why Do We Need Identifiers?
Think About Real Life...
Imagine if everyone in your class had no name. How would the teacher call on someone? "Hey you... no, not you, the other you!" It would be chaos!
Without names (chaos):
"Store 25 in... that thing... you know, the number holder"
With names (clear):
int age = 25;What is an Identifier?
An identifier is simply a name you give to things in your program. Just like people have names, your variables, functions, and other program elements need names so you can refer to them!
Quick Rules Preview
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, AgeInvalid
1name, 2value, 9AgeCan contain letters, digits (0-9), and underscores only
✓Valid
age1, count_2, num_itemsInvalid
my-var, count$, num@2Cannot be a C keyword (reserved word)
✓Valid
myInt, forLoop, integerInvalid
int, for, while, returnCase-sensitive: uppercase ≠ lowercase
age, Age, and AGE are three different identifiers!
No spaces allowed
✓Valid
firstName, first_nameInvalid
first name, my variableLength 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.
!Code Pitfalls: Common Mistakes & What to Watch For
These are the most common mistakes that trip up beginners. Study them carefully to avoid hours of debugging!
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)
08Try It Yourself
Here's a complete program using good identifier names. Notice how readable it is!
1#include <stdio.h>23int main() {4 // Good identifier names - clear and descriptive5 int studentAge = 20;6 float examScore = 85.5;7 char studentGrade = 'B';8 int totalStudents = 30;9 10 // Print with meaningful names11 printf("Student Age: %d years\n", studentAge);12 printf("Exam Score: %.1f%%\n", examScore);13 printf("Grade: %c\n", studentGrade);14 printf("Total Students: %d\n", totalStudents);15 16 return 0;17}Expected Output:
Exam Score: 85.5%
Grade: B
Total Students: 30
Watch Out When Copying Code!
Key Takeaways
- •WHY: Identifiers give names to program elements so we can use them
- •Must start with letter or underscore
- •Can contain letters, digits, underscores only
- •No keywords, no spaces, no special characters
- •C is case-sensitive (age ≠ Age ≠ AGE)
11Naming Best Practices
Use Descriptive Names
Prefer student_count over sc,calculate_average over calcAvg. Code is read more often than written—make it self-documenting.
Follow a Consistent Convention
C traditionally uses snake_case for functions and variables.SCREAMING_CASE is reserved for macros and constants. Pick one style and stick to it throughout your project.
Use Prefixes for Related Items
Group related identifiers with prefixes: str_copy,str_length, str_compare. This makes code organization clear and helps avoid naming conflicts in large projects.
Avoid Abbreviations
Use customer_address not cust_addr. Modern IDEs have autocomplete—longer names cost nothing to type but make code much easier to read.
Be Consistent with Case
Pick one style and stick to it. C traditionally uses snake_case for variables and functions. Use SCREAMING_CASE for macros and constants. Consistent naming makes code predictable and easier to maintain.
Test Your Knowledge
Related Tutorials
Keywords in C
Learn all 32 reserved words in C that have special meaning. These words are reserved by C and cannot be used as variable names.
Data Types & Variables
Learn to store different kinds of data: numbers (int), decimals (float), and characters (char). Understand how much memory each type uses.
Basic Syntax in C
Learn the fundamental building blocks of C programs. Understand the main() function, how to write comments, and why proper formatting makes code readable.
Have Feedback?
Found something missing or have ideas to improve this tutorial? Let us know on GitHub!