446,148 Members | 1,427 Online
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

# a problem about using macro?

 P: n/a I have test the following program, why the result is not 0 2 4 but 0 2 3? Can someone tell me why the same macro produce different result? Thanks! #include #define max(a,b) ((a)>(b) ? (a): (b)) test() { printf("%d\n", 1); } int main(int argc, char* argv[]) { int a=0, b=0; printf("%d %d\n", a, b); max(++a, b); printf("%d %d\n", a, b); max(++a, b+10); printf("%d %d\n", a, b); } Nov 13 '05 #1
6 Replies

 P: n/a "lgn" wrote in message news:bp**********@mail.cn99.com... I have test the following program, why the result is not 0 2 4 but 0 2 3? Can someone tell me why the same macro produce different result? Thanks! #include #define max(a,b) ((a)>(b) ? (a): (b)) test() { printf("%d\n", 1); I think this is an error as 1 is not defined. However a variable (identifier) should start by an alphabet (alphanumeric). } int main(int argc, char* argv[]) { int a=0, b=0; printf("%d %d\n", a, b); max(++a, b); // first attempt printf("%d %d\n", a, b); max(++a, b+10); // second attempt printf("%d %d\n", a, b); Should end with a return statement } The result is 0 2 3 for variable a as the varaible a gets incremented only once in the second attempt unlike 2 times in the first attempt, as the comparison fails in the second attempt. Thanks, Praveen Kumar. Nov 13 '05 #2

 P: n/a lgn wrote: I have test the following program, why the result is not 0 2 4 but 0 2 3? Can someone tell me why the same macro produce different result? Thanks! #include #define max(a,b) ((a)>(b) ? (a): (b)) test() { printf("%d\n", 1); } int main(int argc, char* argv[]) { int a=0, b=0; printf("%d %d\n", a, b); max(++a, b); Evaluates ++a twice so a should be 2. printf("%d %d\n", a, b); max(++a, b+10); Evaluates ++a once so a should be 3. Only one of the conditional operands to the conditional operator is evaluated at any given evaluation of the conditional operator. b+10 is larger than 2 (which is what it is compared against) and so b is returned by the macro. And since b is returned a++ is never evaluated. -- Thomas. Nov 13 '05 #3

 P: n/a sahukar praveen wrote: "lgn" wrote: ....printf("%d\n", 1); I think this is an error as 1 is not defined. You mean the value of 1 is undefined? Or do you mean 1 is not declared? Jirka, int 1; Nov 13 '05 #4

 P: n/a lgn wrote: I have test the following program, why the result is not 0 2 4 but 0 2 3? Can someone tell me why the same macro produce different result? Thanks! #include #define max(a,b) ((a)>(b) ? (a): (b)) test() { printf("%d\n", 1); } int main(int argc, char* argv[]) { int a=0, b=0; printf("%d %d\n", a, b); max(++a, b); a is incremented once to 1, is now greater than b (0) and so is incremented again to 2 for the macro result. printf("%d %d\n", a, b); max(++a, b+10); a is incremented once to 3, is NOT greater than "b+10" (10) and so is not incremented again but stays at 3. printf("%d %d\n", a, b); return 0; } Regards, Ed. Nov 13 '05 #5

 P: n/a Groovy hepcat sahukar praveen was jivin' on Mon, 24 Nov 2003 20:04:28 +0530 in comp.lang.c. Re: a problem about using macro?'s a cool scene! Dig it! "lgn" wrote in messagenews:bp**********@mail.cn99.com... I have test the following program, why the result is not 0 2 4 but 0 2 3? Can someone tell me why the same macro produce different result? Thanks! #include #define max(a,b) ((a)>(b) ? (a): (b)) test() { printf("%d\n", 1);I think this is an error as 1 is not defined. However a variable(identifier) should start by an alphabet (alphanumeric). What on Earth are you talking about? That's an integer constant expression, you fool! Duh! To quote Homer Simpson's brain, "Now look sad and say d'oh." -- Dig the even newer still, yet more improved, sig! http://alphalink.com.au/~phaywood/ "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker. I know it's not "technically correct" English; but since when was rock & roll "technically correct"? Nov 13 '05 #6

 P: n/a sahukar praveen wrote: "lgn" wrote in message news:bp**********@mail.cn99.com... I have test the following program, why the result is not 0 2 4 but 0 2 3? Can someone tell me why the same macro produce different result? Thanks! #include #define max(a,b) ((a)>(b) ? (a): (b)) test() { printf("%d\n", 1); I think this is an error as 1 is not defined. However a variable (identifier) should start by an alphabet (alphanumeric). You're kidding, right? Alex Nov 13 '05 #7

### This discussion thread is closed

Replies have been disabled for this discussion.