Search code examples
cmacrosc-preprocessorparentheses

C macros and use of arguments in parentheses


Example

#define Echo(a)  a
#define Echo(a) (a)

I realize there probably isn’t a significant difference here, but why would you ever want to include the a within parenthesis inside the macro body? How does it alter it?


Solution

  • Suppose you have

    #define mul(x, y)  x * y
    

    What happens if I say:

    mul(a + 5, 6); /* a + 5 * 6 */
    

    Now if I slighlty change the macro:

    #define mul(x, y)  ((x) * (y))
    mul(a + 5, 6); /* ((a + 5) * (6)) */
    

    Remember, the arguments aren't evaluated or anything, only textual substitution is performed.

    EDIT

    For an explanation about having the entire macro in parentheses, see the link posted by Nate C-K.