jeudi 18 février 2016

C Macro Function If...Else Values

I'm getting some strange values from this macro function written in C

#define func(x, y) (x > y) ? y : x

which I thought was equivalent to

int func(int x, int y) {
    if (x > y)
        return y;
    else
        return x;
}

However when I run

int x = 10; 
int y = 9;
int z = func(x, y++); 

printf("x=%d, y=%d, z=%d\n", x, y, z);

I get x=10, y=11, z=10 instead of x=10, y=10, z=9

Any idea what's going on here?

Aucun commentaire:

Enregistrer un commentaire