/** * CSE 374 example program * Demonstration of using preprocessor macros (poorly). */ #include #define TWICE_AWFUL(x) x*2 #define TWICE_BAD(x) ((x)+(x)) #define TWICE_OK(x) ((x)*2) int main(int argc, char **argv) { int x = 1; int y = 2; // This gives 5 instead of 6 (y*2 is executed before adding x) printf("Twice of 1+2 should be 6, is %d\n", TWICE_AWFUL(x+y)); // This gives 3 instead of 2 (the x++ is executed twice). printf("Twice of 1 should be 2, is %d\n", TWICE_BAD(x++)); // This works. printf("Twice of 2+2 should be 8, is %d\n", TWICE_OK(y+y)); return 0; }