ant*_*009 2 c visual-studio-2008 c-preprocessor preprocessor-directive
我有以下代码
#define PROC_ADD
void main(void)
{
while(1)
{
#ifdef PROC_ADD
// Do this code here then undefined it to run the code in the else
// processing work
#undef PROC_ADD
#else
// now that PROC_ADD has been undefined run this code
// processing work
#endif
}
}
Run Code Online (Sandbox Code Playgroud)
但是,它将运行代码。但它不会运行未定义else
后的代码。PROC_ADD
我认为原因可能是您只能在编译时定义和取消定义,而不能在运行时定义和取消定义。不过,我不太确定。
您正在执行的构建时间相当于:
int x = 1;
int main()
{
if (x)
{
...
x = 0;
}
else
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
ifdef等发生在构建时,但对于您的示例,这不是问题。一旦评估了if(运行时或构建时形式),就决定采用哪个分支。做出决定后更改某些内容不会改变该决定。