expressions as associativity
Post on 13-Dec-2015
212 Views
Preview:
DESCRIPTION
TRANSCRIPT
Exercises on Associativity and Precedence rules:
1. Consider the comma operator: “, “
Given that
i=1;
What will be the values of i, sum after the execution of the statement:
++i, sum=sum+i;
2. Fill the value column using the declarations and initializations below:
int i, j, k=3; double x=3.3
Expression Equivalent expression
Value Inference Lessons learnt
i=1, j=2, ++k+i+1;
k!=1, ++x*2.5+3
3. Fill the value column using the declarations and initializations below:
char a= ‘a’, b =’b’; int i=1, j=2;
Expression Value
i==j? a-1 : b-1;
J%3 ==0?i+4:x
J%3?i+4:x
4. Fill the value column using the declarations and initializations below:
int a=1, b=2, c=3; double x=1.0;
Expression Value
a>b&&c<d
a < !b ||!!a
a+b < !c +b
a –x || b*c&&b/a
5. What does the following code fragment will print?
char c =’A’;
int i=5, j=10;
printf(“%d%d%dn”, !c, !!c, !!!c);
printf(“%d%d%dn”, -!i, !-i, ! –i – !j);
printf(“%d%d%dn”, !(6*j +i-c), !i-5, !j-10);
6. Check and analyze the output for the code below:
double x=1e+33, y=0.0001
printf(“%d%d%d\n”, c==’a’, c==’b’,c!=’c’);
printf(“%d\n”, c==’A’&& c< =’B’ || ‘c’);
printf(“%d\n”, 1!=!!c==!!!c);
printf(“%d\n”, x+y >x -y);
7. Check and analyze the output for the code below:
int a=0, b=0, x;
x=0&&(a=b=333);
printf(“%d%d%d\n”, a, b, x);
x=333||(a=++b);
printf(“%d%d%d\n”, a, b, x);
8. Fill the following table below, using precedence rules:
int i=1, j=2, k=3, m=4; double x=3.3
Expression Equivalent expression
Value Inference Lessons learnt
i+=j+k
J*=k=m+5;
9. Check and analyze the output for the code below:
int a, b, c=0;
a=++c;
b=c++;
printf(“%d%d%d\n”, a, b, ++c);
printf(“%d%d%d\n”, a, b, c ++);
top related