Thread: Macro Trouble

  1. #1
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59

    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
    ."

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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)

  3. #3
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    Thank you brew.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. need help with multi line macro usage
    By Cdigitproc1 in forum C Programming
    Replies: 9
    Last Post: 04-29-2005, 09:50 AM
  5. Macro Program
    By daspope in forum Windows Programming
    Replies: 5
    Last Post: 04-25-2004, 04:02 AM