Excerpt
This post covers the specific rules and conventions for valid and invalid variable names in C programming. Learn what makes a variable name legal or illegal.
When writing code in the C programming language, one of the first things you have to do is name your variables. The names you choose for your variables can have a big impact on the readability and maintainability of your code. In C, there are specific rules and conventions for what makes a valid variable name versus an invalid one. This blog post will cover the dos and don’ts of C variable naming.
Introduction to C Variable Naming Conventions
In C, all variable names must adhere to certain syntax rules in order to be valid. Some key points:
- Variable names can contain letters, numbers, and underscores.
- The first character of a variable name must be a letter or underscore.
- C is case-sensitive -
myVar
andmyvar
are different variables. - Variable names cannot contain spaces or special characters like
&
,*
,$
, etc. - Names cannot conflict with C keywords like
if
,else
,int
, etc.
Following these rules allows C to easily distinguish variable names from other tokens in your code. Trying to use an invalid name will result in a compiler error.
Rules for Valid Variable Names
Here are the specific requirements for a legally valid variable name in C:
- The name can begin with a letter (
A
toZ
ora
toz
) or an underscore (_
). - After the first character, the name can also contain digits (
0
to9
). - C variable names are case-sensitive, so
myVar
andmyvar
are different names. - The length of the variable name can be up to 31 characters.
- You cannot use C reserved keywords like
int
,char
,if
, etc. as variable names.
Some examples of valid variable names:
1int pageCount;
2char firstLetter;
3double account_balance;
As long as you follow these rules, the C compiler will accept your variable names.
Below I will provide you with several free online variable conversion tools:
Common Rules for Invalid Names
There are some clear rules that will make your variable name invalid in C:
The name cannot begin with a digit (0-9). It must start with a letter or underscore.
You cannot have spaces or special characters like
&
,*
,$
in the name.Avoid very short names like
x
orq
that don’t indicate the meaning.Don’t use extremely long names of more than 31 characters.
Avoid plural names like
customers
- it’s better to usecustomer
for a single variable.
Some examples of invalid variable names:
1int 1value; // Starts with number
2int full name; // Contains space
3int *ptr; // Contains special character
Trying to use these kinds of names will result in compiler errors.
Examples of Valid and Invalid Names
To solidify your knowledge, here are a few more examples of valid and invalid C variable names:
Valid:
1int wheelsOnBus;
2char firstLetter;
3float account_balance_usd;
Invalid:
1int bus#; // Special character
2float full name; // Space in name
3int else; // C keyword
4int veryVeryLongNameOver31Chars; // Over 31 characters
Take note of the exact reasons that make each invalid name illegal in C.
Why Variable Naming Rules Matter
You may be wondering why C enforces these seemingly arbitrary rules for variable names. There are good reasons:
- The rules help avoid name conflicts with language keywords and built-in identifiers.
- Consistent naming conventions improve the readability of code.
- Names like
index
andcustomerName
are more self-documenting than short ones likei
andcN
. - Invalid names like spaces and special characters can cause major errors.
Adhering to C’s rules for variable naming will make your code cleaner and less error-prone. While simple programs may work even with sloppy naming, it’s best practice to follow the standard conventions.
Summary of Key Points
To recap the key points on C variable naming:
- Variable names begin with a letter or underscore, and can contain letters, numbers, and underscores.
- Names are case-sensitive and can be up to 31 characters long.
- Invalid names include those starting with a number, containing spaces/special characters, or conflicting with keywords.
- Following the standard naming conventions avoids errors and improves readability.
- Choosing descriptive names like
customerCount
makes your code easier to follow.
Be sure to avoid the common mistakes that can arise from something as simple as a variable name. Though the compiler will catch invalid names, following best practices from the start will save you headaches down the road!