Reputation: 78
#include <stdio.h>
void main()
{
int x=5,y=6;
printf("%d%d%d",x++,(y=x++),(x=y++));
}
Can anyone please explain why does this returns 766?
Upvotes: 2
Views: 665
Reputation: 1
This working stack concept as the mention operation LIFO firt right operand goes to the stack that is x = y++ (this is the post increament opratore then first y value assing to x after y will increament that is x=6 )then secound (here x = 6 and y = 7 but y is assign value of x that is 6 then x will increament by 1)and third (here x=7 and after this statement x will increament by 1 ) hance the values are 766.
Upvotes: 0
Reputation: 1
excecution strts frm right to left.So,x=y++,x value becomes 6 and after that y value become seven followed by y=x++,which restoe value of y to 6.After that in x++ statement x value is 7 due to the previous statement(y=x++).And so is the answer 766
Upvotes: -2
Reputation: 115
printf("%d%d%d",x++,(y=x++),(x=y++));
Wanted to add why it's evaluated from the right end as previous answers mentioned. All the arguments of printf are pushed to a stack and then evaluation begins. As stack is LIFO, evaluation begins from the end.
Upvotes: -3
Reputation: 215305
First, if this question, in this format, was given on a beginner C programming course, the course/teacher is a bad one.
The main problem here is that both 'x' and 'y' are modified several times before a sequence point, which is undefined behavior (C99/C11 6.5 §2). This is a severe bug, because anything can happen. Before the ++ mess is removed, there is no telling what this code does. Read this then read it again.
Further, the order of evaluation of function arguments is unspecified behavior. (C99/C11 6.5.2.2 §10). That is, the compiler may evaluate them left-to-right or right-to-left, and we cannot know which order that applies. The compiler does not need to document this! But if you are lucky, it could be documented. You must then read the compiler documentation to see which order of evaluation that applies, before attempting to answer the question. Otherwise you must give two answers.
Further, if this is code for a hosted system, such as a Windows PC, main is only allowed to return 'int' or this code won't compile on a C compiler.
Upvotes: 9
Reputation: 2436
it first evaluates the last expression (x=y++)
. This will return 6 because x is assigned with the value of y which is 6 initially and then it increments y to 1. Now y is 7 and x is 6. x is printed here.
next expression to be evaluated is (y=x++)
. Same as above first assign y= x and increment x to 1. So the values are y is 6 and x is 7. y is printed here.
now x++
. it prints 7 and increments x to 1.
Upvotes: -1
Reputation: 96177
Yes, it's undefined what order the arguements to a function like this are parsed in.
The easiest way to handle a variable number of args function like printf is to start from the end, since you know where the beginning is!
As 'R' points out - it's undefined to have a statement where the evaluation is ambiguous.
eg x++ = x++
Upvotes: 2
Reputation: 215607
You've invoked undefined behavior by modifying x
more than once between sequence points, so you're lucky/unlucky it printed anything at all...
Upvotes: 3