LEARN C PROGRAMMING
In this post, we will learn about the rules for constructing C Variables, in C programming Language.
To learn about the rules for writing C Constants click here.
Types of C variables
A particular type of variable can contain only the same type of constant. For example, an integer variable can hold only an integer constant/value, a real variable can hold only a real constant/value and a character variable can hold only a character constant/value. The rules for constructing these different types of constants are different. However, for constructing variable names of all types, the rules are same. These rules are given below:
Rules for constructing variable names
- A variable name is a combination of about 31 characters including 1 to 26 alphabets, 0 to 9 digits or (_)underscores. Some compilers allow variable names with length up to 247 characters. Still, it would be safer and easier to stick to the rule of 31 characters as creating unnecessarily long variable names adds to more typing effort in writing your code.
- The first character in the variable name has to be an alphabet or underscore(_) compulsorily.
- Commas(,) or blanks( ) are not allowed within a variable name.
- No special symbol (e.g., @,#,$,%,^,& etc) other than an underscore(_) as in average_wt can be used in a variable name.
Since, the maximum allowable length of a variable name is 31 characters, a huge number of variable names can be constructed with the help of the above stated rules. It is a good practice to use this choice in naming variables by using meaningful variable names.
Thus, if we want to calculate simple interest, it is always advisable to construct meaningful variable names like Princi, rate, nyear to represent principal, rate of interest and number of years rather than using the variables a,b,c.
Either primary or secondary variable, the rules for creating these variables names are the same. So the question arises How is C able to differentiate between these variables? To answer this question: For defining a variable it is compulsory for you to declare the data type of the variable that you wish to use in a program, in such a way the C compiler is able to distinguish between the variables names.This type declaration is done at the beginning of the program. Examples of type declaration statements are given below.
Difference between Keywords and Variables
Keywords are those speacial or reserved words of which the C compiler has an in-built or predefined meaning. There are only 32 keywords available in C. These are:
Keywords cannot be used as variable names because if we do so, we are trying to assign a new meaning to the keyword which already has a predefined meaning, which is not allowed.
So this post was about the rules for variables, keywords and the difference between the variables and keywords.