15,882,464 members
See more:
C++
```int _tmain(int argc, _TCHAR* argv[])
{
int a=5;
printf("%d,%d,%d,%d",a++,++a,a--,--a);//4,5,4,5
printf("\na++-%d",a++);//5
printf("++a-%d",++a);//7
printf("a---%d",a--);//7
printf("--a-%d",--a);//5
return 0;
}```

How the output varies when in same line and printed seperately?? Output is given in commented part

Posted
Sergey Alexandrovich Kryukov 23-May-12 1:02am
The question makes no sense. What's not clear, exactly?
Don't you see that this has nothing to do with "separately"?
--SA

## Solution 1

"x++" and "++x" are `postfix` and `prefix` increment operators.
"x++" takes the value of "x", then increments "x" by one, but returns the original value.
So
C#
```x = 6;
a = x++;```
ends up with "a" containing 6, and "x" containing 7.
"++x" increments the value of "x" and then returns the new value.
So
C#
```x = 6;
a = ++x;```
ends up with "a" containing 7, and "x" containing 7.
Although each parameter for `printf` is evaluated separately, they do not have to be evaluated in the left-to-right order you wrote them, hence the odd looking values in your first line.
The others are obvious: a enters as 5, is incremented the the first one after the value is used, enters the second as 6 and is incremented before the value is used and "7" printed. It then enters the third as 7 and is decremented after the value is used, so enters the final one as 6, which is decremented before it is used so it is printed as "5".

VJ Reddy 23-May-12 1:47am
steven8Gerrard 23-May-12 6:20am
I understand printf("%d",a++)etc how it works. My problem is here
printf("%d,%d,%d,%d",a++,++a,a--,--a);//4,5,4,5

In this line right to left order is used so first --a so it should print 4 but 5 is printed,then a-- it should also print 4 and minus. rightly printed 4,then ++a it should print 4 but printed 5 then a++ it should print 5 but printed 4
OriginalGriff 23-May-12 6:39am
No - because the compiler is under no restriction to evaluate parameters in Left-to-Right order.
The order in which they are evaluated is the order in which they are pushed onto the stack - which is not defined by the C specification in order to allow the individual compilers to produce efficient code. However, with *most* compilers, the first (left most) parameter is at the top of the stack, so it will be evaluated last, implying Right-to-Left evaluation order. However, this is not guaranteed and really, really should not be relied upon!

The same applies to evaluation within an expression - you should be vary, vary wary when using more than one auto increment or decrement within any expression as the evaluation order (and thus the auto increment) may not occur in the order you expect!
Aescleal 24-May-12 6:40am
hi Griff, neither the C or C++ standard say that parameters have to be evaluated in the order they're presented to the function. All they say is that all the parameters will be evaluated before they're presented.
Nelek 23-May-12 16:23pm

## Solution 4

At the risk of doing a "me too" no one's mentioned sequence points (again!) in this discussion. Assignment operator unusual behaviour[^] has a previous answer for the last poor mug to get asked this with a reference about them.

Cheers,

Ash

## Solution 6

Hi,
i++ could potentially be slower than ++i, since the old value of i might need to be saved for later use, but in practice all modern compilers will optimize this away.

http://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i[^]

http://www.eskimo.com/~scs/cclass/notes/sx7b.html[^]

Regards,
Alex

## Solution 2

See my answer to This Post[^]

Why do instructors show this example to students? It's totally useless in the real world, is subject to variations from compiler to compiler, and all it does is confuse people who are trying to make sense out of computer programming.

I think instructors do it to show how "clever" they are. A-Holes!

stib_markc 23-May-12 8:17am
exactly! 5+
Nelek 23-May-12 16:24pm
Nice answer. On the other hand... are you sure you didn't want to say A--Holes? ;P

## Solution 3

The order in which the increment and decrement operators will be evaluated cannot be predicted and hence the inconsistency in the values.

Go through :http://stackoverflow.com/questions/181624/c-what-regex-library-should-i-use[^]

It would be better not to print/calculate the increment and decrement operators together.

Hope this helps :)

## Solution 5

This isn't related to only operator behavior, this even links to something called parameter passing or calling conventions.

Please go through the Calling Conventions[^].