I am currently learning C++ and I like to read and explore programs I find on the internet.
I have found this example C++ code, which gives "4 is greater than 2" as an output, but I cannot figure out why:
#include <stdio.h>
#define MAX( a, b ) ( a > b ) ? (a) : (b)
int main()
{
int x = 2, y = 2;
if( MAX( ++x, y ) == x )
{
printf( " %d is greater than %d ", x, y );
}
return 0;
}
What I do not understand is: if you look at the segment which states
if( MAX( ++x, y ) == x )
it is supposed to increase the X variable by 1, then call MAX; at that point X should be 3. Instead, when you compile it the output is as aforementioned.
I have done some research on how the ++ operator works ( Incrementing in C++ ) but I could not get the solution anyway. Could you please explain me why this happens?
Thanks in advance for your help.