Thursday 14 June 2012

Variables

C Variables :
A variable is a data name that may be used to store a data value. Unlike constants that remain unchanged during the execution of a program. a variable may take different values at different times during execution. In Chapter 1, we used several variables. For instance, we used the variable amount in Sample Program 3 to store the value of money at the end of each year (after adding the interest earned during that year).
A variable name can be chosen by the programmer in a meaningful way so as to reflect its function or nature in the program. Some examples of such names are:

Average
height
Total
Counter_ 1
class_strength
As mentioned earlier, variable names may consist of letters, digits, and the underscore(_) charac-ter, subject to the following conditions:

1. They must begin with a letter. Some systems permit underscore as the first character.
2. Standard recognizes a length of 31 characters. However, length should not be normally more than eight characters, since only the first eight characters are treated as significant by many compilers.
3. Uppercase and lowercase are significant. That is, the varible Total is not the same as total or TOTAL.
4. It should not be a keyword.
5. White space is not allowed.
Some examples of valid variable names are:


John                       Value                       T_raise

Delhi                          xl                       ph_value

mark                         suml                        distance

Invalid examples include:

123 (area)
25th
Further examples of variable names and their con-ectness are given in Table .


If only the first eight characters are recognized by a compiler, then the two names
average_height 
average_weight


mean the same thing to the computer. Such names can be rewritten as
avg_height and avg_weight or
ht_average and wt_average


without changing their meanings.



No comments:

Post a Comment