Variable Names
C variable names are identifiers used to hold values in your program. They act as labels for memory locations where data is stored. Choosing good variable names is crucial for readability, maintainability, and understanding your code.
Example
#include
int main() {
// Good variable name
int minutesPerHour = 60;
// OK, but not so easy to understand what m actually is
int m = 60;
printf("%d\n", minutesPerHour);
printf("%d", m);
return 0;
}
Output
60
60
The general rules for naming variables are:
- Start with a letter, underscore (_), or dollar sign ($): Variable names cannot begin with numbers or other special characters.
- Valid characters: Variables can contain letters, numbers, and underscores only.
- Case Sensitivity: C is case-sensitive, so myVariable and myvariable are considered different.
- Names cannot contain whitespaces or special characters like !, #, %, etc.
- Followed by Letters, Digits, or Underscores: After the initial letter or underscore, subsequent characters can be letters (uppercase or lowercase), digits (0-9), or underscores.
- Avoid Keywords: Do not use C keywords (reserved words) as variable names. For example, int, char, for, etc., are reserved and cannot be used as variable names.