Click here to Skip to main content
14,297,880 members
Rate this:
Please Sign up or sign in to vote.
See more:
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


Guidance will be helpful
Posted
Comments
   
The question makes no sense. What's not clear, exactly?
Don't you see that this has nothing to do with "separately"?
--SA
Rate this:
Please Sign up or sign in to vote.

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
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
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".
   
Comments
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
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
   
Nice answer
Rate this:
Please Sign up or sign in to vote.

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
   
Rate this:
Please Sign up or sign in to vote.

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.

Maybe it will help you:

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

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

Regards,
Alex
   
Rate this:
Please Sign up or sign in to vote.

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!
   
Comments
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
Rate this:
Please Sign up or sign in to vote.

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 :)
   
Rate this:
Please Sign up or sign in to vote.

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[^].
   
Comments
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100