macro特殊用法

#的使用方法(因为老是忘,所以记录于此):

Quoting macro arguments(当字符串引用)

#define QUOTEME(x) #x

the code

printf("%s\n", QUOTEME(1+2));

will expand to

printf("%s\n", "1+2");

Token concatenation(Token当做字符串连接)

#define MYCASE(item,id) \
case id: \
item##_##id = id;\
break

switch(x) {
MYCASE(widget,23);
}

The line MYCASE(widget,23); gets expanded here into

case 23:
widget_23 = 23;
break;


Standard predefined positioning macros

// debugging macros so we can pin down message origin at a glance
#define WHERESTR "[file %s, line %d]: "
#define WHEREARG __FILE__, __LINE__
#define DEBUGPRINT2(...) fprintf(stderr, __VA_ARGS__)
#define DEBUGPRINT(_fmt, ...) DEBUGPRINT2(WHERESTR _fmt, WHEREARG, __VA_ARGS__)
//...

DEBUGPRINT("hey, x=%d\n", x);

或者:
#define DEBUGPRINT2(args...) fprintf(stderr, ## args) //注意args与##之间有空格,特殊用法




posted @ 2011-10-16 17:49  Jack204  阅读(421)  评论(0编辑  收藏  举报