Friday, January 6, 2017

Constants, Variables and Keywords in C Programming Language



Constants, Variables and Keywords
The alphabets, digits and special symbols when properly combined form constants, variables and keywords. Let us now understand the meaning of each of them. A constant is an entity that doesn't change, whereas, a variable is an entity that may change. A keyword is a word that carries special meaning.
In any C program we typically do lots of calculation. The results of these calculations are stored in computer's memory. Like human memory, the computer's memory also consists of millions of cells. The calculated values are stored in these memory cells. To make the retrieval and usage of these values easy these memory cells (also called memory locations) are given names. Since the value stored in each location may change, the names given to these locations are called variable names. Let us understand this with the help of an example. 
Memory Locations


Since the location whose name is X can hold different values at different times X is known as variable (or a variable name). As against this, 3 or 5 do not change, hence are known as constants.
Now that we understand the constants and the variables, let us see what different types of constants and variables exist in C.

Types of C Constants
C constants can be divided into two major categories:
a) Primary constants
b) Secondary constants
These constants are further categorized as shown in figure:
Types of Constants


At this stage, we would restrict our discussion to only Primary constants, namely, Integer, Real and Character constants. Let us see the details of each of these constants. For constructing these different types of constants, certain rules have been laid down. These rules are as under:

Rules for constructing Integer Constants
a) An integer constant must have at least one digit.
b) It must have a decimal point.
c) It can be either positive or negative.
d) If no sign precedes an integer constant, it is assumed to be positive.
e) No commas or blanks are allowed within an integer constant.
f) The allowable range for integer constants is -2147483648 to + 2147483647.
Truly speaking, the range of an integer constant depends upon the compiler. For compilers like Visual Studio, gcc, it is -2147483648 to + 2147483647, whereas for compilers like Turbo C or Turbo C++ the range is -32768 to 32767.

Rules for constructing Real constants
Real constants are often called Floating point constants. The real constants could be written in two forms - Fractional form and Exponential form.
Following rules must be observed while constructing real constants expressed in fractional form:
a)  A real constant must have at least one digit.
b) It must have a decimal point.
c) It could be either positive or negative.
d) Default sign is positive.
e) No commas or blanks are allowed within a real constant.
The exponential form is usually used if the value of the constant is either too small or too large. It, however, doesn't restrict us in any way from using exponential form for other real constants.
In exponential form the real constant is represented in two parts. The part appearing before 'e' is called mantissa, whereas the part following 'e' is called exponent. Thus 0.000342 can be written in exponential form as 3.42e-4 (which in normal arithmetic means 3.42*10^-4).

Following rules must be observed while constructing real constants expressed in exponential form:
a) The mantissa part and the exponential part should be separated by letter e or E.
b) The mantissa part may have a positive or negative sing.
c) Default sign of mantissa part is positive.
d) The exponent must have at least one digit, which must be a positive or negative integer. Default sign is positive.
e)  Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
Rules for constructing character constants
a) A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas.
B) Both the inverted commas should point to the left. For example, 'A' is a valid character constant whereas 'A' is not. 

Types of C Variables
A particular type of variable can hold only the same type of constant. For example, an integer variable can hold only an integer constant, a real variable can hold only a real constant and a character variable can hold only a character constant. The rules for constructing different types of constants are different. However, for constructing variable names of all types, the same set of rules applies. These rules are given below.

Rules for constructing variable names
a) A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some compilers allow variable names whole length could be up to 247 characters. Still, it would be safer to stick to the rule of 31 characters. Do not create unnecessarily long variable names as it adds to your typing effort.
b) The first character in the variable name must be an alphabet or underscore.
c)  No commas or blanks are allowed within a variable name.
d) No special symbol other than an underscore (as in gross_sal) can be used in a variable name.
These rules remain same for all the types of primary and secondary variables. Naturally, the question follows........how is C able to differentiate between these variables? This is a rather simple matter. C compiler is able to distinguish between the variable names by making ti compulsory for you to declare the type of any variable name that you wish to use in a program. This type declaration is done at the beginning of the program. Following are the examples of type declaration statements:
Ex int si,m_hra;
Float bassal;
Char code;
Since, the maximum allowable length of a variable name is 31 characters, an enormous number of variable names can be constructed using the above-mentioned rules. It is a good practice to exploit this enormous 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 prin, roi, noy to represent principle, rate of interest and number of years rather than using the variables a,b,c.

C Keywords
Keywords are the words whose meaning has already been explained to the C compiler (or in a broad sense to the computer). The keywords cannot be used as variable names because if we do so, we are trying to assign a new meaning to the keyword, which is not allowed. Some C compilers allow you to construct variable names that exactly resemble the keywords. However, it would be safer not to mix up the variable names and the keywords. The keywords are also called 'Reserved Words'.
There are only 32 keywords available in C, and they are:
Auto      double  int               struct
Break    else        long           switch
Case      enum    registger     typedef
Char      extern   return         union
Const    float       short          unsighed
Continue             for              signed   
Default  goto       sizeof        volatile
Do          if           static           while

Note that compiler vendors (like Microsoft, Borland, etc) provide their own keywords apart from the ones mentioned above. These include extended keywords like near, far, asm etc. thought it has been suggested by the ANSI committee that every such compiler specific keyword should be preceded by two underscores (as in __asm), not every vendor follows this rule.

No comments:

Post a Comment