Computer Concept & Programming In C - Unit II - 11

Q.12 Explain differences between precedence and associativity of operators with suitable example.                                                                    (AKTU. 2008-09)
Ans. Consider the following C program
# include <stdio.h>
# include <conio.h>
void main ( )
{
     int a = 3, b = 6, c = 4, x;
     x = a * b + c;
     printf (“%d”, x);
     getch ( );
}
The output of the program is 22.
How precedence and associativity of operators work together can be shown as follows.
It is clear that a = 3, b = 6 & c = 4.
Now consider expression x = a * b + c;
Which now appears as x = 3 * 6 + 4;
Now the role of operator precedence is to decide that in -
Step 1 : 3 will be multiplied to 6 since * is highest which evaluates to be 18
Step 2 : (3 * 6) will be added to 4 since precedence of + is lower than * but higher than = thus 18 + 4 = 22 is evaluated and NOT as 3*(6 + 4) = 3*10 = 30.
Step 3 : 22 will be assigned to x since the operator precedence of = is lowest amongst *, + & = so finally x has value of 22 stored in it which is printed.
In each of the proceeding 3 steps the sole of associativity of operator was as follows.
(i) In step 1 the associativity of * was from left to right which means that 3 is to be considered first and then 6.
i.e. direction of progress of (*) multiplication is from left to right.
(ii) In step 2 the associativity of + was from left to right which means that 18 was to be considered first and then 4 (i.e. value of C)
i.e. direction of progress of (+) addition is from left to right.
(iii) In step 3 the associativity of = was from right to left which means that 22 was to be considered first and then x (i.e. variable in which value is to be assigned.
i.e. direction of progress of (=) assignment operator is from right to left.
Hence we conclude that.
(i) Operator precedence decides the order in which various operators of an expression will operate. AND
(ii) Operator associativity decides the direction in which a particular operator will start operating in.

Q.13 What is the difference between initialization and assignment of a variable.                                                                                             (AKTU. 2009-10)
Ans. When we declare a variable in C say for an example :
int a;
float a;
double a; etc.
The following things take place.
(1) A sequence of bytes (memory) is allocated which depends upon the basic data type i.e. 2 byte for int, 4 byte for float and so on.
(2)  The memory located has a starting address which is identified by the identifier ( in this case) followed by the data type. Now there may be two cases.
(i) The allocated memory is filled with some value which is arbitrary.
(ii) The user deliberately stores some value in the allocated memory.
Now comes assignment and initialization.
The statements like a = 5; or a + = 5; or a - = 5; etc are all assignment operators. In assignment of a variable a variable which is declared stores some value.
The initialization means when the variable is assigned a value for the first time.
The difference between the initialization and assignment is best understood by the following example.
# include <stdio.h>
# include <conio.h>
void main ( )
{
    int i = 0; /* Assignment First Time */
    clrscr ( );
    printf (“\n i is initialized to % d”, i);
    do
{
    i = i + 1;
    printf (“\n Again i is assigned % d”, i);
}
    while (i < 10);
    getch ( );
}
Thus initialization is also the assignment but for the first time. Moreover, the initialization is done just for one time but assignment may be done many time.