a problem about using macro?

L

lgn

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 <stdio.h>
#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);
}
 
T

Thomas Stegen

lgn said:
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 <stdio.h>
#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.
 
E

Ed Morton

lgn said:
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 <stdio.h>
#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.
 
S

sahukar praveen

lgn said:
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 <stdio.h>
#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.
 
P

Peter Shaggy Haywood

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!
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"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top