C Notes
Complete guide to identifiers in C — naming rules, conventions, valid and invalid examples, and the difference between identifiers and keywords.
When you write a C program, you create variables, functions, arrays, structures, and many other elements. Each one needs a name so you can refer to it later in your code. These names are called identifiers. Choosing good identifiers is one of the simplest yet most impactful things you can do to make your code readable and maintainable.
Rules for Naming Identifiers in C
C has strict rules about what constitutes a valid identifier. If you break any of these rules, the compiler will reject your code with an error.
Rule 1: Must Begin with a Letter or Underscore
The first character of an identifier must be:
- An uppercase letter (A–Z)
- A lowercase letter (a–z)
- An underscore (
_)
It cannot start with a digit (0–9).
int name; // ✓ starts with letter
int _count; // ✓ starts with underscore
int Age; // ✓ starts with uppercase letter
// int 2ndPlace; // ✗ starts with digit — INVALIDRule 2: Can Contain Letters, Digits, and Underscores
After the first character, identifiers can include:
- Letters (A–Z, a–z)
- Digits (0–9)
- Underscores (
_)
No other characters are allowed — no spaces, hyphens, periods, or special symbols.
int student1; // ✓ digits allowed after first character
int max_value; // ✓ underscores allowed
int totalMarks2; // ✓ mix of letters and digits
// int my-var; // ✗ hyphen not allowed
// int total$; // ✗ dollar sign not allowedRule 3: Cannot Be a C Keyword
Identifiers must not clash with any of C's 32 reserved keywords. The compiler uses these words for specific purposes and won't let you redefine them.
int value; // ✓ not a keyword
int number; // ✓ not a keyword
// int int; // ✗ 'int' is a keyword
// int return; // ✗ 'return' is a keyword
// int while; // ✗ 'while' is a keywordRule 4: Case-Sensitive
C treats uppercase and lowercase letters as different characters. Age, age, and AGE are three completely different identifiers.
#include <stdio.h>
int main() {
int age = 25;
int Age = 30;
int AGE = 35;
// These are three DIFFERENT variables
printf("age = %d\n", age);
printf("Age = %d\n", Age);
printf("AGE = %d\n", AGE);
return 0;
}age = 25 Age = 30 AGE = 35
Rule 5: No Length Limit (But Significance Varies)
The C standard guarantees that at least the first 31 characters of an internal identifier are significant (63 characters in C99). For external identifiers (across files), at least the first 6 characters must be significant in C89 (31 in C99).
In practice, most modern compilers support much longer significant names, but keeping identifiers reasonably short is good practice anyway.
Rule 6: No Spaces Allowed
Identifiers cannot contain spaces. If you need a multi-word name, use underscores or camelCase.
int first_name; // ✓ use underscore
int firstName; // ✓ use camelCase
// int first name; // ✗ spaces not allowedValid and Invalid Identifiers — Quick Reference
| Identifier | Valid? | Reason |
|---|---|---|
sum | ✓ | Starts with letter, all lowercase |
_temp | ✓ | Starts with underscore |
MAX_VALUE | ✓ | Uppercase with underscore |
student1 | ✓ | Digit after first character is fine |
calculateArea | ✓ | camelCase, all valid characters |
my_var_2 | ✓ | Mix of letters, digits, underscore |
__internal | ✓* | Valid but reserved by convention |
2ndYear | ✗ | Cannot start with a digit |
my-variable | ✗ | Hyphens not allowed |
total marks | ✗ | Spaces not allowed |
float | ✗ | Keyword — reserved word |
price$ | ✗ | Special characters not allowed |
for | ✗ | Keyword — reserved word |
hello@world | ✗ | @ symbol not allowed |
Note: Identifiers starting with double underscore (__) or underscore followed by an uppercase letter (_A) are reserved for the compiler and standard library. Avoid using them in your own code.
Naming Conventions and Best Practices
While C doesn't enforce naming conventions (only rules), following established conventions makes your code professional and readable.
Common Conventions
| Convention | Style | Used For | Example |
|---|---|---|---|
| camelCase | firstName | Variables, functions | totalAmount, getUserInput() |
| snake_case | first_name | Variables, functions | total_amount, get_user_input() |
| UPPER_SNAKE_CASE | MAX_SIZE | Constants, macros | BUFFER_SIZE, PI |
| PascalCase | StudentRecord | Structs, typedefs | LinkedList, TreeNode |
| Prefix notation | g_count, p_data | Globals, pointers | g_totalUsers, p_head |
Best Practices
Tips for Choosing Good Names
- Be descriptive —
numberOfStudentsis better thann - Be concise —
maxLenis fine;maximumLengthOfTheStringBufferis too long - Use consistent style — pick camelCase or snake_case and stick with it
- Avoid single-letter names — except for loop counters (
i,j,k) - Make names pronounceable —
stuCntis harder to discuss thanstudentCount - Avoid abbreviations unless universally understood (
max,min,lenare fine)
Identifiers vs Keywords — Key Differences
| Feature | Identifiers | Keywords |
|---|---|---|
| Definition | User-defined names | Pre-defined reserved words |
| Purpose | Name program entities | Define language syntax/structure |
| Flexibility | Can be anything (following rules) | Fixed — cannot be changed |
| Case | Case-sensitive (user decides) | Always lowercase |
| Examples | sum, age, printResult | int, if, return, while |
| Total count | Unlimited | 32 (in C89) |
Scope and Identifier Visibility
The same identifier name can be used in different scopes without conflict:
#include <stdio.h>
int value = 100; // global 'value'
void display() {
int value = 50; // local 'value' — shadows global
printf("Inside display: %d\n", value);
}
int main() {
int value = 25; // local to main
printf("Inside main: %d\n", value);
display();
return 0;
}Inside main: 25 Inside display: 50
Each value is a separate variable despite sharing the same identifier name — they exist in different scopes.
Identifiers in Different Contexts
#include <stdio.h>
#define MAX 100 // MAX is a macro identifier
typedef int Integer; // Integer is a typedef identifier
enum Color { RED, GREEN, BLUE }; // Color, RED, GREEN, BLUE are identifiers
struct Point { // Point is a struct tag identifier
int x; // x is a member identifier
int y; // y is a member identifier
};
int globalVar = 10; // globalVar is a variable identifier
int add(int a, int b) { // add, a, b are identifiers
return a + b;
}
int main() { // main is an identifier
struct Point p1; // p1 is a variable identifier
p1.x = 5;
p1.y = 10;
Integer sum = add(p1.x, p1.y);
printf("Sum: %d\n", sum);
printf("Max allowed: %d\n", MAX);
return 0;
}Sum: 15 Max allowed: 100
Common Mistakes with Identifiers
Mistake 1: Starting with a Digit
// int 1st_student; // ERROR
int first_student; // CORRECT
int student_1; // CORRECTMistake 2: Using Special Characters
// int total-marks; // ERROR (hyphen interpreted as minus)
// int email@id; // ERROR
int total_marks; // CORRECT
int email_id; // CORRECTMistake 3: Using Keywords
// int case; // ERROR
// float break; // ERROR
int testCase; // CORRECT
float breakPoint; // CORRECTMistake 4: Confusing Case
int count = 10;
// printf("%d", Count); // ERROR: 'Count' is not declared
printf("%d", count); // CORRECT: case must match exactlyInterview Questions on Identifiers in C
Q1: What is an identifier in C? Give examples.
An identifier is a user-defined name given to program elements such as variables, functions, arrays, and structures. Examples: age, calculateSum, MAX_SIZE, struct Student. Identifiers must follow naming rules — start with a letter/underscore, contain only alphanumeric characters and underscores, and not be a keyword.
Q2: What is the maximum length of an identifier in C?
The C89 standard guarantees that the first 31 characters of an internal identifier are significant. C99 extended this to 63 characters. In practice, most compilers support even longer names, but only the standard-defined number of characters are guaranteed to be distinguishing.
Q3: Are identifiers case-sensitive in C? Explain with an example.
Yes, C identifiers are case-sensitive. total, Total, and TOTAL are three different identifiers. If you declare int count; and later try to use Count, the compiler will report an "undeclared identifier" error.
Q4: Why can't identifiers start with a digit?
This is a parser design decision. If identifiers could start with digits, the compiler would have difficulty distinguishing between numeric literals (like 123) and identifiers (like 123abc). By requiring the first character to be a letter or underscore, the compiler can immediately determine whether a token is a number or a name.
Q5: What identifiers are reserved by convention in C?
Identifiers starting with a double underscore (__) or an underscore followed by a capital letter (_X) are reserved for the compiler implementation and standard library. Additionally, identifiers starting with a single underscore at file scope are reserved. Using them in user code may cause conflicts.
Summary
- Identifiers are user-defined names for variables, functions, arrays, and other program elements.
- They must start with a letter or underscore, followed by letters, digits, or underscores.
- Identifiers cannot be C keywords and are case-sensitive.
- At least 31 characters are significant in C89 (63 in C99).
- Following consistent naming conventions (camelCase, snake_case, UPPER_CASE) improves code readability.
- Avoid starting identifiers with double underscores or underscore + capital letter (reserved).
- Good identifier names are descriptive, concise, consistent, and pronounceable.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Identifiers in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, basics, identifiers, identifiers in c programming
Related C Programming Topics