main()
{
int i=5;
printf("%d%d%d%d%d",i++,i--,++i,--i,i);
}
Answer:
45545
Thanks in advance.
The order that function parameters are evaluated is unspecified behavior. Different compilers can do it in different ways, and this is a mess of side effects as the parameters are evaluated. The only way to know for sure what's happening in this situation is to take apart the generated executable and see what the assembly is doing. But you won't learn anything useful; every other compiler could do something else. Your own compiler could do it differently next time, depending on all sorts of things.
As per Moschops. In my opinion, this is a textbook exercise to teach you the perils in performing evaluations (functions) on arguments in another function. This problem, caused by others, has caused me many sleepless nights living inside a debugger!
In any case, some compilers will evaluate arguments left-to-right, and others right-to-left. IE, this is one of those "we left it up to the compiler writers to decide" things that the C/C++ standards committees agreed on. IE, they couldn't decide to agree, so they left it up to someone else! :-)
The real essence of elegant programming is not to rely on order of evaluation..Moreover use gdb to debug the code by creting multiplr in-line breakpoint in the printf statement..
Debugging is not an answer. The behaviour of that code is undefined in the standards, so any compiler.now or in the future, can implement any order of evaulation it wants. Confirming that it works with one particular compiler is just setting yourself up for a disaster later. The code is bad code, downright wrong code, and should never be allowed to live.
Actually some modern version of lldb/gdb helps you to put breakpoint on several threads. Most of the compilers execute every expressions in pritnf within a bundle of sub-threads & those debuggers as they let you put breakpoints on those threads, just by debugging & simultaneously checking the value of the variable, you can make sure which expression is executed how & which value is further taken for calulation. So modern debugger is the answer.
So modern debugger is the answer.
Only if the question is "what did this exact compiler do this exact time that I compiled this exact code using these exact settings on this exact computer?" If this is out of idle curiosity, fine, but it's effectively useless information to have.
I note that this question has come up many times across the internet. Is there some company or school out there asking this question as if it has a "real", meaningful answer?
Agreed but the actual question was if we can explain the output and how it was formed which is different from the question that what will be the output. Hence I mentioned that there are debuggers which can explain you better than us that why the output is like what he mentioned..
if i is 5
than printing
i++ will print 5 and from the next line i will be 6
i-- will print 5 and from the next line i will be 4
++i will first increment i to 6 and than print it(6)
--i will first decrement i to 4 and than print it(4)
i will just print i(5) without changing it
if i is 5
than printing
i++ will print 5 and from the next line i will be 6
i-- will print 5 and from the next line i will be 4
++i will first increment i to 6 and than print it(6)
--i will first decrement i to 4 and than print it(4)
i will just print i(5) without changing it
So you're saying 55645? Take a look at this:
http://ideone.com/6qEPH6 - 45555 when I last looked at it. The point is it's unspecified behaviour. You do not know what order the parameters will be evaluated in because that is left up to the person who writes the compiler. Every compiler could be different. There is no correct answer. You can work out, for a given compiler what it chose to do at one point in time on one machine, but it could be different the next time you compile it. There is no correct answer because the question makes no sense; the C language does not specify what should happen.
Also, you seem to misunderstand what i++ does. It does not change the value of i "from the next line". It changes it at the point that i++ is evaluated. i++ evaluates to i as it was before you began evaluating i++, but the value of i is changed right there - it does not change "from the next line".
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.