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.

Click here and here.

Its undefined, google sequence points.

Aia I like your avatar.

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.

commented: Unless it's remote-controlled ;) +10

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);

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...

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

Works for me :)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.