0 votes 0 votes anyone explain this prog?? how to output is evaluvated?? int main() { int x = 10, y; y = (x++, printf("x = %d\n", x), ++x, printf("x = %d\n", x), x++); printf("y = %d\n", y); printf("x = %d\n", x); return 0; } Hira Thakur asked Jun 22, 2017 Hira Thakur 916 views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
Best answer 2 votes 2 votes In the statement: y = (a,b,c,d,e); comma "," operator acts as a sequence point and thus evaluates from left to right and result of the expression is rightmost value. But the statement, y = a , b, c; is same as : (y = a),b,c; because "," has the lowest precedence. Now in the expression (x++, printf("x = %d\n", x), ++x, printf("x = %d\n", x), x++), First argument is x++, x will be incremented by 1 for the second argument and 10 will be passed in first argument. So the second argument will have 11 as the value of x. In third argument x will be incremented by 1(pre-increment), so 12 will be passed in 3rd argument. The fourth argument will take 12 as the value of x, The last argument is post-increment, so 'x 'will remain 12 and 'y' will be assigned 12. After assignment, x will be incremented by 1. So, the output will be: x = 11 x = 12 y = 12 x = 13 Comma in C/C++: http://www.geeksforgeeks.org/?p=8482 neeraj33negi answered Jun 22, 2017 edited Jun 22, 2017 by neeraj33negi neeraj33negi comment Share Follow See all 2 Comments See all 2 2 Comments reply Arjun commented Jun 22, 2017 reply Follow Share You have to say that "," comma operator is a sequence point unlike "comma separator" in function argument list and hence there is no undefined behaviour here. But even then such meaningless codes are out of GATE scope. 0 votes 0 votes neeraj33negi commented Jun 22, 2017 reply Follow Share Sir, I have changed my answer a bit and added a link for a detailed study. 1 votes 1 votes Please log in or register to add a comment.
0 votes 0 votes The output will be as follows: x = 11 x = 12 y = 12 x = 13 To understand the program you just need to know two things: y = x++; will copy the value of x to y first and then increments the value of x by 1 y = ++x; will increment the value of x by 1 and then copies the value of x to y Pramod Devireddy answered Jun 22, 2017 Pramod Devireddy comment Share Follow See all 0 reply Please log in or register to add a comment.