C/C++中取消宏定义

来源:互联网 发布:ims数据怎么读取 编辑:程序博客网 时间:2024/06/05 22:52

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


原创粉丝点击