Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c define multiline macro?

Tags:

c

macros

#define DEBUG_BREAK(a)\
    if ((a)) \
{\
    __asm int 3;\
}

I have defined a macro as above, and try to use it

#include "test_define.h"
int main()
{
    DEBUG_BREAK(1 == 1);
    return 0;
}

But this sample will not compile. The compiler would complain the parenthesis is not closed. If I add another } in the end of the source file, it compiles.

What's wrong with this macro?

like image 896
Jichao Avatar asked May 22 '13 06:05

Jichao


2 Answers

The macro

#define DEBUG_BREAK(a)\
    if ((a)) \
    __asm int 3;

works fine but

#define DEBUG_BREAK(a)\
    if ((a)) \
{\
    __asm int 3;\
}

doesn't! And I think anyone could guess why!! The new line operator is the problem making guy!

It takes

 __asm int 3;\
}

as

__asm int 3; }

where ; comments out what follows it (in assembly). So we will miss a } then.

like image 187
raj raj Avatar answered Sep 20 '22 20:09

raj raj


Check there is no white space after each backslash. I often fall for this.

You might even need a single space before the backslash.

like image 22
Bathsheba Avatar answered Sep 20 '22 20:09

Bathsheba