“From today on, study hard and make progress every day”
Repetition is the best method for memory; spend one minute each day to remember the basics of C language.
“Essential Knowledge Points for C Language Beginners: 100 Articles Series“
Variable Naming in C Language: From Syntax Rules to Best Practices
1. Basic Rules for Variable Naming
Variable naming in C language must adhere to the following syntax rules:
- 1. Can only contain letters, numbers, and underscores
- 2. Cannot start with a number
- 3. Cannot use C language keywords (such as int, float, etc.)
- 4. Case-sensitive
2. Examples of Valid and Invalid Naming
Valid Naming:
int age;
float average_score;
char _address[50];
double payment2023;
Invalid Naming:
int 2year; // Error: starts with a number
float return; // Error: uses a keyword
char name-age; // Error: contains illegal character (-)
3. Naming Convention Recommendations
- 1. Lowercase letters: use lowercase for regular variables (e.g.,
<span>counter</span>) - 2. Underscore separation: connect multiple words with underscores (e.g.,
<span>student_count</span>) - 3. Constants in uppercase: use uppercase for macro definitions and constants (e.g.,
<span>MAX_SIZE</span>) - 4. Avoid abbreviations: unless widely recognized (e.g.,
<span>num</span>instead of<span>number</span>) - 5. Avoid single characters: except for loop variables (e.g.,
<span>i,j,k</span>)
4. Practical Code Example
#include <stdio.h>
// Macro definition in uppercase
#define MAX_STUDENTS 50
int main() {
// Regular variables in lowercase + underscore
int student_count = 0;
float average_grade = 85.5;
// Array naming reflects content
char student_names[MAX_STUDENTS][30];
// Loop variable allows single character
for(int i = 0; i < 10; i++) {
printf("Processing record %d\n", i);
}
return 0;
}
5. Common Errors
- 1. Case confusion:
int Age = 10;
printf("%d", age); // Error: Age and age are considered different variables
- 2. Misuse of reserved words:
int char = 5; // Error: char is a keyword
- 3. Cross-platform issues:
// Names starting with an underscore followed by uppercase are reserved for the compiler
int _System = 10; // Not recommended, may cause conflicts
6. Naming Length Recommendations
- 1. Variable name length should be between 8-20 characters
- 2. Too short (e.g.,
<span>x</span>) lacks descriptiveness - 3. Too long (e.g.,
<span>number_of_students_in_class_2023</span>) affects readability
7. Special Case Naming
- 1. Pointer variables: suggest adding
<span>p</span>prefix or<span>ptr</span>suffix
int *p_age; // Method one
int *age_ptr; // Method two
- 2. Boolean variables: use
<span>is_</span>or<span>has_</span>prefix
int is_valid;
int has_children;
8. Conclusion
Good variable naming should:
- 1. Comply with syntax rules
- 2. Clearly express the variable’s purpose
- 3. Maintain consistent style
- 4. Avoid ambiguity and confusion
———- End ———-
[Special Statement: This article is original or authorized by the author, some content and images are sourced from the internet and AI, please feel free to use, opinions are for learning reference only~~]

“If you like C, please like it”
Click the bottom right corner to view
“