leecny2012 发表于 2014-3-3 17:13:30

keil for arm的#if RST==0 #else #endif中RST未定义居然不报错

keil for arm的
#if RST==0
//程序1
#else
//程序2
#endif

RST未定义居然不报错,RST按照0处理了,这可真是危险哪。怎样才能让KEIL在这种情况下能报错?

binaimei2007 发表于 2014-3-3 17:27:29

楼主真不要随便怀疑这么成熟的编译器。
可以看看C语言条件编译与预处理方面的文章

山外メ雲ジ 发表于 2014-3-3 18:06:15

binaimei2007 发表于 2014-3-3 17:27
楼主真不要随便怀疑这么成熟的编译器。
可以看看C语言条件编译与预处理方面的文章 ...

这个不是编译器的bug,而是C99里有明确的规定。没定义的都替换为0

山外メ雲ジ 发表于 2014-3-3 18:09:24

Prior to evaluation, macro invocations in the list of preprocessing tokens that will become the controlling constant expression are replaced (except for those macro names modified bythe defined unaryoperator),justasinnormaltext.If thetoken defined is generated as a result of this replacement process or use of the defined unary operator does not match one of the two specified forms prior to macro replacement, the behavior is undefined.After allreplacementsduetomacroexpansionandthe defined unary operator have been performed, all remaining identifiers are replaced with the pp-number 0, andtheneachpreprocessingtokenisconvertedintoatoken.

yyts 发表于 2014-3-3 18:10:28

前面自己判断RST是否已定义

leecny2012 发表于 2014-3-3 18:17:08

本帖最后由 leecny2012 于 2014-3-3 18:18 编辑

没有怀疑C编译器,只是想找到解决问题的方法。

山外メ雲ジ 发表于 2014-3-3 18:50:19

leecny2012 发表于 2014-3-3 18:17
没有怀疑C编译器,只是想找到解决问题的方法。

多一个#ifdefRST ,这样就可以了

dr2001 发表于 2014-3-3 19:26:40

#if defined(RST) && (RST == 0)
...
页: [1]
查看完整版本: keil for arm的#if RST==0 #else #endif中RST未定义居然不报错