A variable is an entity whose value can change during program execution. A variable can be thought of as a symbolic representation of address of the memory space where values can be stored, accessed and changed. A specific location or address in the memory is allocated for each variable and the value of that variable is stored in that location.
Each variable has a name, data-type, size, and the value it stores. All the variables must have their type indicated so that the compiler can record all the necessary information about them, generate the appropriate code during translation and allocating required space in memory.
Every programming language has its own set of rules that must be observed while
writing the names of variables. If the rules are not followed, the compiler reports
compilation error. Rules for Constructing Variable Name in C language are listed
below:
a. Variable name may be a combination of alphabets, digits or underscores.
Sometimes, an additional constraint on the number of characters in the
name is imposed by compilers in which case its length should not exceed
8 characters.
b. First character must be an alphabet or an underscore (_).
c. No commas or blank spaces are allowed in a variable name.
d. Among the special symbols, only underscore can be used in a variable name.
e.g.: emp_age, item_4, etc.
e. No word, having a reserved meaning in C can be used for variable name.
Note that C language is a case-sensitive language which differentiates between lower case and upper case. Thus, CAT is different from Cat, cAT, CaT. Although any word complying with the rules cited above can be used as the variable name, it is advised tha you create variable names that have some meaning. Thus, you may chose sum as the variable name for storing sum of numbers rather than choosing X.
Type Declaration
C language is strongly typed language, meaning that all the variables must be declared before their use. Declaration does two things:It tells the compiler what the variable name is.
It specifies what type of data the variable will hold.
In C language, a variable declaration has the form:
Here
e.g.-
a. int i, j, k; //creates integer variables i,j and K
b. char ch; //creates a character type variable ch
Once variable has been declared in the above manner, the compiler creates a space in the memory and attaches the given name to this space This variable can now be used in the program.
A value is stored in a variable using assignment operation. Assignment is of the form:
Variable-name =
j = 5; i = 0 ;
C also allows assignment of a value to a variable at the time of declaration. It takes the following form:
e.g. : int I = 5;
This declaration not only creates an interior type variable I, but also stores a value 5 at the same time.
The process of assigning initial values to the variable is known as initialization. More than one variable can be initialized in one statement using multiple assignment operators.
e.g.: i. int i = 5, ]=3;
ii . int j, m; j = m = 2; However, there is an exception worth noting. Consider the following example:
int i, j = 2, k ; The assignments will be made as follows: i = 0 j = 2
k =1063 (a garbage value, unutilized) Let us consider some of programming examples to illustrate the matter further.
Example 2
/* Example of assignments */ /'* declaration */
int al, bl; /* declaration & assignment */
int var = 5;
int a, b 1 6, c;
/* declaration & multiple" assignment */
int p, q, r, s;
p = q=r = s = 5;
values stored in various variables are:
var = 5
a = 0, b = 6
c = garbage value
p = 5, q = 5, r = 5, s = 5
No comments:
Post a Comment