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 Himani Srivastava asked Nov 5, 2015 Himani Srivastava 6.6k views answer comment Share Follow See all 2 Comments See all 2 2 Comments reply Sandip Shaw commented Nov 5, 2015 reply Follow Share is it c? 0 votes 0 votes Himani Srivastava commented Nov 5, 2015 reply Follow Share yes ans is c.. 0 votes 0 votes Please log in or register to add a comment.
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 Umang Raman answered Nov 5, 2015 Umang Raman comment Share Follow See all 7 Comments See all 7 7 Comments reply Himani Srivastava commented Nov 5, 2015 reply Follow Share It is fine but we can use associativity and precedence of operators also na..In that case answer should be 6 0 votes 0 votes Arjun commented Nov 5, 2015 reply Follow Share token generation order may not be same as execution order. To put it simply, in an arithmetic expression a variable cannot be modified and reused or else answer is undefined. 0 votes 0 votes Himani Srivastava commented Nov 5, 2015 reply Follow Share But sir is it syntatically correct?? 0 votes 0 votes Arjun commented Nov 5, 2015 reply Follow Share syntactically it is correct- parser won't complain. But logically it is invalid- not a valid code as in the sense answer is not defined by C. 1 votes 1 votes Himani Srivastava commented Nov 5, 2015 reply Follow Share ok sir thanku 1 votes 1 votes Rohan Ghosh commented Nov 6, 2015 reply Follow Share Sir,can we say that it's sometimes 6 sometimes 8 depending on the compiler because of the sequence points? 0 votes 0 votes Arjun commented Nov 6, 2015 reply Follow Share yes but it can be any other value too, though C option is the best pick. 0 votes 0 votes Please log in or register to add a comment.
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. hacker16 answered Nov 23, 2017 edited Nov 23, 2017 by hacker16 hacker16 comment Share Follow See all 2 Comments See all 2 2 Comments reply Arjun commented Nov 23, 2017 reply Follow Share Compiler has "no associativity". Execution order also has "no associativity". When a parse tree is made obeying precedence and associativity, then the execution can happen in any order within it - that is all. 1 votes 1 votes hacker16 commented Nov 23, 2017 reply Follow Share I mean to say if compiler, is going to used left associativity then answer would be different and if it is going to used right associativity then answer would be different. plz correct me if i am wrong. @arjun sir 1 votes 1 votes Please log in or register to add a comment.