Vote count:
0
I'm currently taking an online C/C++ tutorial which dates back to 2012. The tutorial never really paid attention to the order in which arguments passed to printf() were evaluated until it starts discussing macros, in which the instructor mentions that arguments are evaluated right to left. So when the following code is run:
#include <stdio.h>
#define MAX(a, b) ( (a) > (b) ? (a) : (b) )
int increment() {
static int i = 42;
i += 5;
printf("increment returns %d\n", i);
return i;
}
int main( int argc, char ** argv ) {
int x = 50;
printf("max of %d and %d is %d\n", x, increment(), MAX(x, increment()));
printf("max of %d and %d is %d\n", x, increment(), MAX(x, increment()));
return 0;
}
The resulting output IN THE TUTORIAL, as expected, is:
increment returns 47
increment returns 52
max of 50 and 52 is 50
increment returns 57
increment returns 62
increment returns 67
max of 50 and 67 is 62
which is clearly a right-to-left evaluation. However, when I run the exact same code on my own computer I get:
increment returns 47
increment returns 52
increment returns 57
max of 50 and 47 is 57
increment returns 62
increment returns 67
increment returns 72
max of 50 and 62 is 72
which seems to only be possible if the evaluation of x, increment(), MAX(x, increment()) is done left-to-right. Can anyone shed some light on this for me? its quite confusing. Could this have anything to do with compiling the code using LLVM vs GCC?? I'm using LLVM with Mavericks 64-bit. I'm not sure what the tutorial instructor is using but given the date the tutorial was made I assume its OSX Lion 64-bit.
C: printf is evaluating the arguments left to right instead of right to left
Aucun commentaire:
Enregistrer un commentaire