Variable Names in C
Rules for Naming Variables
• Names must begin with a letter or underscore.
• They may include letters, numbers, and underscores but no spaces or special characters.
• They cannot be a C keyword (e.g., `int`, `if`, `return`).
• C is case-sensitive: `Age`, `age`, and `AGE` are all different.
• Avoid leading underscores in your own names—identifiers beginning with an underscore followed by an uppercase letter or another underscore are reserved for the implementation; a single leading underscore is reserved at file scope.
Valid Names | Invalid Names |
---|---|
count, student_age, _balance | 2count, my-variable, float |
Keywords vs Identifiers
Keywords are reserved words with special meaning in C (e.g., `int`, `char`, `while`).
They cannot be used as variable names. A few common keywords: `auto`, `break`, `case`, `char`, `const`, `continue`, `do`, `double`, `else`, `enum`, `extern`, `float`, `for`, `goto`, `if`, `inline`, `int`, `long`, `register`, `restrict`, `return`, `short`, `signed`, `sizeof`, `static`, `struct`, `switch`, `typedef`, `union`, `unsigned`, `void`, `volatile`, `while`.
Best Practices
• Use meaningful names (e.g., `studentScore` instead of `ss`).
• Follow a consistent naming style (snake_case or camelCase).
• Avoid overly long or cryptic names.
• Avoid names that differ only by case (e.g., `total` vs `Total`).
• Prefer `const` variables for typed constants and `#define` UPPER_SNAKE_CASE for macros.
• For portability, stick to ASCII letters, digits, and underscores in identifiers.
Common Naming Conventions
Category | Convention | Example | Notes |
---|---|---|---|
Local variables / params | lower_snake_case | student_count | |
Functions | lower_snake_case | compute_average() | |
Struct / enum names | PascalCase or snake_case | struct PlayerStats | Pick one style per project |
Constants (macros) | UPPER_SNAKE_CASE | #define MAX_SIZE 256 | Prefer const when possible |
Typedef aliases | Suffix or PascalCase | typedef unsigned long usize; | Avoid new names ending in _t for POSIX portability |
Good vs Bad (with Comments)
#include <stdio.h>
int main(void) {
int student_count = 30; // good: descriptive, snake_case
int maxScore = 100; // good: descriptive, camelCase
int x1 = 5; // acceptable but vague
// int 2items = 10; // invalid: cannot start with a digit
// int my-score = 10; // invalid: hyphen not allowed
// int float = 3; // invalid: 'float' is a keyword
printf("%d %d %d\n", student_count, maxScore, x1);
return 0;
}
30 100 5
Case Sensitivity Demonstration
C distinguishes identifiers by case. These are three different variables:
#include <stdio.h>
int main(void) {
int age = 18;
int Age = 21;
int AGE = 30;
printf("age=%d Age=%d AGE=%d\n", age, Age, AGE);
return 0;
}
age=18 Age=21 AGE=30
Constants: const vs Macro
Prefer typed constants when possible; use macros for conditional compilation or when a true compile-time constant is required in preprocessor contexts.
#include <stdio.h>
#define MAX_STUDENTS 32 // macro constant (no type)
const int pass_mark = 60; // typed constant
int main(void) {
int group_size = MAX_STUDENTS;
printf("group=%d pass=%d\n", group_size, pass_mark);
return 0;
}
group=32 pass=60
Avoid Ambiguous Characters
Pick names that are easy to read aloud and in certain fonts:
• Avoid single-letter names like `l` (ell) or `O` that can be confused with `1` or `0`.
• Prefer `index` over `i` in long scopes; reserve short names for tiny loops.