All You Need to Know About Valid Variable Names in C

There are some naming conventions used in programming languages. Here I am explaining some of the valid variable names used in C programming language. Examples related to all these cases have been shown using Code::Blocks IDE.

1. You cannot use a number in the beginning of a variable.

The code does not compile and shows error if I use a number in the beginning of the variable name. 1var is not a valid variable name.

2. DON’T use underscore in the beginning of a variable but you can use it.

The code compiles even if I used the underscore in the beginning of the variable because its a valid variable name. But its not recommended since numbers starting with an underscore are reserved for system use. If you use an underscore in the beginning for your variable it may cause problem in code execution.

3. You cannot use special characters in the variable name.

Again, the code does not compile because its not a valid variable name.

4. You cannot use keywords as your variable names.

There are certain keywords fixed for the programming language like the name of the data types in C: int, float, doublechar, etc. But you can use them by changing their font-size because C is case-sensitive. So if you use the keyword int as INT or InT there is no problem.

Here I have used two keywords which should not be used as variable names because their font-size is changed and they are joined with an underscore. You can also keep their font-size same as before and just add the underscore in between that would also work.

5. Spaces are not allowed in variable names.

I have added a space between the variable name so the code does not compile because the variable name is not valid.

6. Don’t use long variable names.

Long variable names are valid names, but they don’t look good for sure. So it’s better not to use them.

Related Posts