预处理程序相等性测试,这是标准吗? [英] Preprocessor equality test, is this standard?
问题描述
我已经在项目首选项中设想了其中之一
I had envisaged one of these in the project preferences
-
测试
=主机
-
测试
=目标
-
测试
完全没有定义
TESTING
=HOST
TESTING
=TARGET
TESTING
not defined at all
我的问题是后者。
似乎不是
#if TESTING==HOST
#error "HOST defined" // add temporarilly for testing porpoises
#endif
我需要编码
#ifdef TESTING
#if TESTING==HOST
#error "HOST defined" // add temporarilly for testing porpoises
#endif
#endif
我坚信这不是标准行为,因为如果未定义 TESTING
,那么它肯定不会等于 HOST
,而我不需要使用GCC编译器进行额外的 #ifdef TESTING
。
I am convinced that this is not-standard behaviour, since if TESTING
is not defined then it certainly doesn't equal HOST
, and I do not need that extra #ifdef TESTING
with the GCC compiler.
但是,当我使用Atmel AVR St udio(我认为是基于MS Visual Studio的),有必要在几十个地方添加初始的 #ifdef TESTING
: (
However, when I use the Atmel AVR Studio (which I think is based on MS Visual Studio), it is necessary to add that initial #ifdef TESTING
in a few dozen places :-(
看起来我别无选择,但我只是想知道是否有任何C标准都要求这样做。
It looks like I have no choice, but I just wondered if any C standard acually requires this.
推荐答案
#if TESTING==HOST
如果未定义 TESTING
,则
等效于:
#if 0==HOST
根据C标准:
(C99,6.10.1p4)由于宏扩展和定义的一元变量而进行的所有替换之后操作符已执行,所有剩余的标识符(包括与关键字在词法上相同的标识符)都替换为pp-number 0
(C99, 6.10.1p4) "After all replacements due to macro expansion and the defined unary operator have been performed, all remaining identifiers (including those lexically identical to keywords) are replaced with the pp-number 0""
这篇关于预处理程序相等性测试,这是标准吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!