ant*_*009 0 c macros gcc c-preprocessor
gcc 4.4.4 c89
我试图定义一些东西.如果它被定义我想做某事,否则我想做一些不同的事情.
#define PARSE_STRING
for(i = 0; i < NUMBER_OF_STRINGS; i++) {
#if defined (PARSE_STING)
/* run code for parsing the strings */
#else
/* run code that doesn't parse the strings
}
#endif
Run Code Online (Sandbox Code Playgroud)
当我在我的函数中尝试上面的代码时,我似乎在我的代码中得到了其他错误.但是,如果我注释掉#define PARSE_STRING它编译好了.我只是想知道我需要#define PARSE_STRING吗?
非常感谢任何建议,
======编辑更新的解决方案
相反,这样做会更好吗?
#define PARSE_STRING
for(i = 0; i < NUMBER_OF_STRINGS; i++) {
#if defined (PARSE_STRING)
/* run code for parsing the strings */
#elif defined (NO_PARSE_STRING)
/* run code that doesn't parse the strings
#endif
}
Run Code Online (Sandbox Code Playgroud)
您已经将预处理指令的交错与函数体的开头和结尾混合在一起:
}
#endif
Run Code Online (Sandbox Code Playgroud)
应该是
#endif
}
Run Code Online (Sandbox Code Playgroud)