In the very simple C program below, what is the expected compiler error? gcc is giving me 1 whereas MSVC 2013 is giving me 2.
#define foo
#define bar (defined(foo))
#if bar
#error 1
#else
#error 2
#endif
My questions are hopefully equally simple:
- What does the C spec say about the value of defined()? I can't seem to find anything that talks about setting its value to another macro.
- The actual code is not something I have control over and "#if bar" is used all over the place. What is the simplest way to change the #define so that #if bar will work as "expected" in MSVC? The only thing I can think of is to expand it out:
.
#ifdef foo
#define bar 1
#else
#define bar 2
#endif
The C spec says:
(emphasis mine) However, how macro replacement is very complex, and I think MSVC is defining
foo
asdefined(bar)
which is undefined behavior, wheras GCC is definingfoo
as1
correctly. Since MSVC is then in undefined behavior, it does strange things.As you say, the easiest fix is