Click here to Skip to main content
15,882,464 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
int _tmain(int argc, _TCHAR* argv[])
	int a=5;
return 0;

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

Guidance will be helpful
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"?

"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.
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.
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".
Share this answer
VJ Reddy 23-May-12 1:47am    
Nice answer. 5!
steven8Gerrard 23-May-12 6:20am    
I understand printf("%d",a++)etc how it works. My problem is here

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


Share this answer
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.

Maybe it will help you:[^][^]

Share this answer
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!
Share this answer
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
The order in which the increment and decrement operators will be evaluated cannot be predicted and hence the inconsistency in the values.

Go through :[^]

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

Hope this helps :)
Share this answer
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[^].
Share this answer
Aescleal 24-May-12 6:36am    
No it's not - it's related to a concept called sequence points. These are points in the program where the observable behaviour of the program is defined and making assumptions about the order of execution between these points is undefined.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900