且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用C空宏定义

更新时间:2023-12-01 08:47:25

这仅仅是扩大,好了,没事的宏。然而,现在宏已经被定义,你可以用检查#如果定义(或 #IFDEF )有否被定义。

It is simply a macro that expands to, well, nothing. However, now that the macro has been defined you can check with #if defined (or #ifdef) whether it has been defined.

#define FOO

int main(){
    FOO FOO FOO
    printf("Hello world");
}

将扩大到

int main(){

    printf("Hello world");
}

有某些情况下这是非常方便,例如额外的调试信息,这些信息你不想在你的发布版本显示:

There are certain situations where this comes in very handy, for example additional debug information, which you don't want to show in your release version:

#ifndef NDEBUG
#define DEBUG_MSG(x) print(x)
#else
#define DEBUG_MSG(x) 
#endif

int main(){
    DEBUG_MSG("Entering main");
    /* ... */
}

如果 NDEBUG (没有调试)已定义的宏, DEBUG_MSG 将扩大到什么,否则你会获得进入主。需要注意的是杂散分号; 是没有问题的。这是一个有效的空语句。

If the macro NDEBUG (no debug) has been defined, DEBUG_MSG will expand to nothing, otherwise you will get Entering main. Note that the stray semicolon ; isn't a problem. It's a valid empty statement.