6,637 views
3 votes
3 votes

The following code fragment:  

int x, y= 2, z, a; 

 x= (y* =2) + (z= a =y);

 printf(“%d”, x);  

(a) prints 8

(b) prints 6

(c) prints 6 or 8 depending on the compiler implementation

(d) is syntactically wrong

2 Answers

1 votes
1 votes
Compiler Scans from left to right and generate token.
so first (y* =2); = y = y*2 this will be computed and y value will change to 4
then (z= a =y); so z= 4
finally x = 4+4 = 8
1 votes
1 votes

let,

X = Y + Z

IF Compiler Uses Left Associative, then it will Solve Expression Y first.

IF Compiler Uses Right Associative, then it will Solve Expression Z first.

So depending on compiler implementation answer can be 6 or 8.

edited by

Related questions

1 votes
1 votes
2 answers
1
Umang Raman asked Oct 7, 2015
1,120 views
int main (){ int a=5,b=3; printf("%d", a+++++b); // 5 +'s }Please Explain.
1 votes
1 votes
3 answers
2
ajit asked Oct 1, 2015
1,580 views
what is the output of the following c code?#include<stdio.h void main() { int index; for(index=1;index<=5;index++) { printf("%d",index); if(index==3) continue; } }a)1245b...
2 votes
2 votes
5 answers
3
debanjan sarkar asked Sep 3, 2015
6,403 views
void myfunc(int X){ if(X 0) myfunc( X ); printf("%d", X); } int main(){ myfunc(5); return 0; }0,0,1,2,3,44,3,2,1,04,3,2,1,0,00,1,2,3,4
0 votes
0 votes
1 answer
4
Amit kanodia asked Feb 4
121 views
class A:def_init__(s): self.s=sdef print():passa = A('John')a.print()