Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

c - 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

My problem is with the latter.

It seems that instead of

#if TESTING==HOST
#error "HOST defined"  // add temporarilly for testing porpoises
#endif

I need to code

#ifdef TESTING   
#if TESTING==HOST
#error "HOST defined"  // add temporarilly for testing porpoises
#endif
#endif

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.

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 :-(

It looks like I have no choice, but I just wondered if any C standard acually requires this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
#if TESTING==HOST

If TESTING is not defined, then

it is equivalent to:

#if 0==HOST

From the C Standard:

(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""


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...