Can anyone help me solve this question
void main()
{
int num, a=5;
num = -a-- + + ++a;
printf("%d %d\n", num, a);
}
Answer: 0 5
But how? Can anyone explain the logic behind?
Can anyone help me solve this question
void main()
{
int num, a=5;
num = -a-- + + ++a;
printf("%d %d\n", num, a);
}
Answer: 0 5
But how? Can anyone explain the logic behind?
Once upon a time I watched two aggressive Boston drivers trying to park in the same parking space, such that they actually crashed their cars into each other. Something very much like that happens here.
There are other things wrong with this code, but let's just focus on what gets stored into a
. If we imagine that the expression a-- + ++a
gets evaluated from left to right, the first thing that happens is the a--
part. a
started out as 5, so a--
has the value of 5, with a note to store 4 into a
. Imagine that this note is given to the driver of the first car for delivery.
Next we get to ++a
. Assume for the moment that the message to store 4
into a
hasn't gotten through yet. So ++a
has the value 6, with a note to store 6 into a
. Imagine that this note is given to the driver of the second car for delivery.
One big question is, what is the final value of num
, but I'm not worried about that for now. Let's just ask, what is the final value of a
? We've got one car driver trying to set it to 4, and one trying to set to 6. But there's only one parking space labeled a
. Maybe the first driver gets there first and sets it to 4, or maybe the second driver gets there and sets it to 6. Or maybe they get there at the exact same time, and crash into each other, and a
gets set to a twisted piece of metal from the second car's front bumper. (Perhaps that twisted piece of metal looks a little like the number 5, so printing 5
was the best that printf
could do.)
As I said, there are other things wrong with this code. For one thing, we shouldn't have imagined that it gets evaluated from left to right; there's no guarantee about that at all. We also don't know if the ++a
part should operate on the original value of a
, or the value affected by a--
. In fact, we have no idea what this expression should do, and the perhaps surprising fact is, the compiler doesn't know, either! Different compilers will interpret an expression like this differently, giving wildly different answers. None of the answers are right, and none are wrong, because this expression is undefined.
The expression is undefined because there are two different attempts inside it to assign a new value to a
. (You'll find more formal definitions of undefined behavior in the linked answers.) So a simple rule for avoiding undefined behavior is, "don't try to set a variable's value twice in one expression".
You might be thinking that this expression ought to have a well-defined interpretation. You might be thinking that it should obviously be evaluated from left to right. You might be thinking that a--
should store the new value into a
immediately, such that it would be guaranteed to be the value seen by "later" parts of the expression. You might think those things, and they might be true about some other programming language, but not C. C does not guarantee that expressions are evaluated strictly left to right, and it does not guarantee that a--
or ++a
update a
's value immediately. Trying to update a
's value twice in one expression leads to undefined behavior, and undefined behavior means that anything can happen.