用C空宏定义 [英] Empty macro definition in C

查看:587
本文介绍了用C空宏定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设空的宏定义

#define FOO

有效期是标准的C?如果有,是什么 FOO 这个定义后?

推荐答案

这仅仅是扩大,好了,没事的宏。然而,现在宏已经被定义,你可以用检查#如果定义(或 #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.

这篇关于用C空宏定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆