include<stdio.h>
void main()
{
int i=5;
int c;
c=i++ + ++i + i++ + --i;
printf("\n%d",i++ + ++i + i++ + --i);
printf("\n%d",c);
}

The o/p that I'm getting is
31
23

while my friend got
34
20.


Also why are the 2 (printf ' c 'and printf 'expression') values different. Is it because printf evaluation is different from how a normal expression would be evaluated?

thanks in advance.

Edited 7 Years Ago by Nick Evan: Add code-tags

include<stdio.h>
void main()
{
int i=5;
int c;
c=i++ + ++i + i++ + --i;
printf("\n%d",i++ + ++i + i++ + --i);
printf("\n%d",c);
}
The o/p that I'm getting is
31
23

while my friend got
34
20.


Also why are the 2 (printf ' c 'and printf 'expression') values different. Is it because printf evaluation is different from how a normal expression would be evaluated?

thanks in advance.

It probably depends on how the compiler evaluates the expressions...left to right
or right to left...

It probably depends on how the compiler evaluates the expressions...left to right
or right to left...

Or up and down. It doesn't matter. It is undefined behavior.
If a plane doesn't have a pilot, it doesn't matter if people speculate about the capacity of fuel tanks to make the trip, or the condition of weather in the area, or even how much weigh can handle. It can not make the trip.

Comments
Unless it's remote-controlled ;)

ohk...

but why are the two values , printf("\n%d",i++ + ++i + i++ + --i); and printf("\n%d",c);
different?

ohk...

but why are the two values , printf("\n%d",i++ + ++i + i++ + --i); and printf("\n%d",c);
different?

Because you don't set i back to 5

c=i++ + ++i + i++ + --i;
i = 5;////set i back to five
printf("\n%d",i++ + ++i + i++ + --i);
printf("\n%d",c);

Edited 7 Years Ago by gerard4143: n/a

Or up and down. It doesn't matter. It is undefined behavior.
If a plane doesn't have a pilot, it doesn't matter if people speculate about the capacity of fuel tanks to make the trip, or the condition of weather in the area, or even how much weigh can handle. It can not make the trip.

He wanted to know why and that's probably why. One compiler set up the assembled code to read the values left to right and one set up the values to be read right to left. Like you said the language leaves this behavior undefined so its up to the compiler designers to decided which method L -> R or R -> L.

And your analogy...Is that supposed to make sense...

Edited 7 Years Ago by gerard4143: n/a

ohk...

but why are the two values , printf("\n%d",i++ + ++i + i++ + --i); and printf("\n%d",c);
different?

If you would take a little time to understand the information I gave you via links, in the second post, you probably would have not need to ask that question.

This article has been dead for over six months. Start a new discussion instead.