-
Macro Trouble
Hi, i'm having touble with this macro. The compiler is telling me that I have a stray backslash. I'm new to this preprocessor stuff and haven't been able to find a good, straightforward guide to it. How can I clean up this piece of code?
Code:
#define MAX(x, y, z) \ \
{ \
int t = x; \
if (y > t) \
t = y; \
if (z > t) \
t = z; \
return t; \
}
int main()
{
int a = 1, b = 2, c = 3;
printf("\n%i\n\n", MAX(a,b,c));
return 0;
}
The compiler yelled, "
Code:
[fl76a06@cisweb ex05]$ make
cc -c -o ex05.o ex05.c
ex05.c: In function `main':
ex05.c:22: error: stray '\' in program
ex05.c:22: error: syntax error before '{' token
ex05.c:22: error: `t' undeclared (first use in this function)
ex05.c:22: error: (Each undeclared identifier is reported only once
ex05.c:22: error: for each function it appears in.)
ex05.c: At top level:
ex05.c:22: error: syntax error before ')' token
make: *** [ex05.o] Error 1
."
-
You are using MAX as if it was an expression, but it is not an expression, as it is enclosed in curly braces. You also have a return, which is meaningless because this is not a function. You need:
Code:
#define MAX(a, b) ((a)>=(b)?(a):(b))
#define MAX3(a, b, c) MAX(MAX(a, b), c)
-