linux c 取消宏定义

C/C++中可以用 #undef xxx 来取消 宏xxx 的定义。

举例如下

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    // xxx is defined
    bool b = true;


#define xxx

#ifdef xxx
    cout << "xxx is defined #1" << endl;     // this line is printed
#endif

    // undefine xxx
#ifdef xxx
 #undef xxx
    b = true;
#endif

#ifdef xxx
    cout << "xxx is defined #2" << endl;     // not printed
#endif

    // define xxx again
    if (b)
    {
#define xxx
    }

#ifdef xxx
    cout << "xxx is defined #3" << endl;     // printed
#endif

    return 0;
}

结果输出:

xxx is defined #1
xxx is defined #3

发布了57 篇原创文章 · 获赞 540 · 访问量 486万+

猜你喜欢

转载自blog.csdn.net/whatday/article/details/104085098