- What Are Variable Names?
Every variable in C must have a unique name.
These names are called identifiers — they help the compiler and programmer identify the variable.
Example:
int age = 20;
float totalAmount = 450.75;
char grade = ‘A’;
- Short vs Descriptive Names
You can name variables with short names like x or y,
but descriptive names make your code easier to read and understand.
Example:
// ✅ Good (clear meaning)
int minutesPerHour = 60;
// ⚠️ Not clear
int m = 60;
💡 Tip: Always use meaningful names in your programs. It makes your code professional and easier to maintain.
- Rules for Naming Variables
|
Rule |
Explanation |
Example |
|
✅ Can contain |
Letters (A–Z, a–z), digits (0–9), and underscore _ |
student1, total_score, value_3 |
|
✅ Must begin with |
A letter or underscore (_) |
_name, count |
|
⚠️ Case-sensitive |
myVar ≠ myvar (different variables) |
– |
|
❌ Cannot contain spaces or symbols |
No spaces or special characters like ! # % |
❌ total amount, ❌ my#age |
|
❌ Cannot use reserved words |
Words like int, float, return, for, etc. are keywords in C |
❌ int = 5; (invalid) |
Quick Examples
✅ Valid names:
int age;
float totalAmount;
char first_name;
int number1;
❌ Invalid names:
int 1num; // Cannot start with number
int my name; // No spaces
int total$; // No special characters
int float; // Reserved word
- Best Practice Tips 💡
- Use lowercase letters for normal variables → age, sum, marks.
- Use camelCase for descriptive names → studentName, marksCount.
- Never mix uppercase/lowercase randomly → ❌
Summary Table
|
Type |
Example |
Valid? |
Reason |
|
Starts with letter |
age |
✅ |
Valid |
|
Starts with underscore |
_total |
✅ |
Valid |
|
Starts with number |
2ndValue |
❌ |
Invalid |
|
Contains space |
total amount |
❌ |
Invalid |
|
Uses symbol |
total$ |
❌ |
Invalid |
|
Keyword used |
int |
❌ |
Invalid |