Variable Naming Conventions
Variable Names
To use variables in the user C program, he must know how to create variable names.
In C, variable names must adhere to the following rules:
- The name can contain letters, digits, and the underscore character (_).
- The first character of the name must be a letter. The underscore is also a legal first
- character, but its use is not recommended.
- Case matters (that is, upper and lowercase letters). Thus, the names count and Count refer to two different variables.
- C keywords can′t be used as variable names. A keyword is a word that is part of the C language.
Variable Name Legality
- The following list contains some examples of legal and illegal C variable names:
Variable Name | Legality |
Percent | Legal |
y2x5_fg7h | Legal |
annual_profit | Legal |
_1990_tax | Legal but not advised. |
saving#account | Illegal: Contain the illegal character #. |
double | Illegal: Is a C Keyword. |
9winter | Illegal: First character is a digit. |
- Using an underscore to separate words in a variable name makes it easy to interpret.
- The second style is called camel notation.
- Instead of using spaces, the first letter of each word is capitalized.
- Instead of interest rate, the variable would be named Interest Rate.
- Camel notation is gaining popularity, because it′s easier to type a capital letter than an underscore.
- We use the underscore because it′s easier for most people to read.
- User should decide which style, he wants to adopt.