Click here to Skip to main content
15,898,991 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
#include<stdio.h>
#include<math.h>
int main()
{
    int a=2,c;
    c=pow(++a,a++);
    printf("%d",c);
}



output is 16
Posted
Updated 11-Jan-14 16:51pm
v2
Comments
Ron Beyer 11-Jan-14 23:03pm    
And the question is?

If you read this you will see how function parameters are commonly pushed onto the stack starting from the right hand side.

http://www.csee.umbc.edu/~chang/cs313.s02/stack.shtml[^]

It appears you compiler uses this convention as do ms c compilers

For pow(x,y) y would be pushed first then x.

Now in your function call the rightmost expression is a++ (postfix) so the value of a == 2 is pushed first then incremented.
The variable a now has the value 3.
The next expression is ++a so a is incremented again making it 4 and the value is then pushed on the stack.

Thus for the function call to pow the stack now holds the value of 2 for y and 4 for x.

4^2=16.
 
Share this answer
 
v2
Comments
Andreas Gieriet 13-Jan-14 7:31am    
This is one of the possible solutions, but it is an arbitrary solution. No guarantee that it will be the same all the time. The compiler is not bound to give that particular result. See my Solution #3: this is undefined behaviour.
A compiler might even behave differntly depending on the number of arguments of a function, or any other unknown quirks...
Advise: don't do side effects on function call parameters.
Cheers
Andi
Evaluation of function call parameters is "unsequenced" according to the language specification.
Side effects (like pre-/post- -increment/-decrement) on multiple arguments is therefore "undefined behaviour".

I.e. a compiler is free to evaluate function arguments in any order before executing the function body - no guarantee that it is evaluated from left to right, nor from right to left, nor any other sequence.

The only solution to this: don't do (multiple) side effects on the same object in one full expression.

E.g. the following are also undefined behaviour:
C++
int  a = ...
int *b = ...
a = b[a++];  // undefined behaviour
a = a++ + 1; // undefined behaviour


The language specification has several "undefined behaviour". Undefined behaviour is: "behavior for which this International Standard imposes no requirements".

Cheers
Andi
 
Share this answer
 
Comments
[no name] 12-Jan-14 19:51pm    
I think it is good that you have reminded us of this.
Andreas Gieriet 13-Jan-14 7:37am    
Not everything that is possible should also be done.
It's better to know when leaving secure paths and venturing onto thin ice ;-)
Cheers
Andi
If you are wondering why its 16 and not 27, here's why:

First, it pushes a++ on the function stack, which pushes 2.
Then it post-increments a++ turning it into 3.
Then it pre-increments a++ turning it into 4.
Then it pushes 4 onto the stack.

4 raised to the power of 2 (squared) is 16.

Oddly enough, in .NET, this gives you a value of 27, because in .NET it behaves a little differently. First it pre-increments a turning it into 3, pushes 3 onto the stack then increments it to 4, then the equation becomes 4 cubed or 27.
 
Share this answer
 
Comments
Sunil Kumar Das 11-Jan-14 23:27pm    
thanks
Andreas Gieriet 13-Jan-14 7:33am    
The results differ since the behaviour is undefined. Any compiler is free to evaluate the function arguments in any order, e.g. not even bound to evaluate left-right nor right-left. See Solution #3.
Cheers
Andi

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