3

I'm telling other people that cc -DFOO is just the same as cc -DFOO=1 but I'm not quite confident if all compilers support this. So is it a standard for C compilers?

(As inspired by the accepted answer, found the recent POSIX c99 standard, 2016 edition. Just for reference.)

pynexj
  • 19,215
  • 5
  • 38
  • 56

2 Answers2

4

It's not a standard for C compilers, though it is a standard for POSIX-compliant systems. See the cc manual.

autistic
  • 1
  • 3
  • 35
  • 80
0

The ISO standard requires only that an undefined macro evaluates to zero. So while it then makes sense to make a defined value non-zero, it is not required. A value of 1 would also make sense as a non-zero value since that is the value of a true boolean expression when cast to an integer.

So while it is not 'standard' it is probably the most likely and least perverse implementation. However you shouldn't really advocate the explicit testing of such macros by value since to do so demonstrates distinct lack of care. Such macros should be tested for definition only rather than value.


Note that an expression of the form #if defined FOO FOO resolves to #if 0L if FOO is undefined and #if 1L if FOO is defined, but that is an evaluation of defined FOO and says nothing about the valuue of FOO.

The following have well defined behaviour in the case that FOO is defined but with no explicitly assigned value:

#ifdef FOO
#ifndef FOO
#if defined FOO
#if !defined FOO
#if FOO != 0
#if FOO == 0

That is probably enough options not to advocate:

#if FOO == 1
Clifford
  • 88,407
  • 13
  • 85
  • 165
  • Any links to the *ISO standard* you're referring to? – pynexj Apr 01 '15 at 08:18
  • @whjm : An trusting the answer given at http://stackoverflow.com/questions/5085392/what-is-the-value-of-an-undefined-constant-used-in-if-c in this case. – Clifford Apr 01 '15 at 10:57
  • @Clifford I have no idea what you are trying to say with the "The following are defined in this situation" block, could you re-word it perhaps? – M.M Apr 01 '15 at 12:17
  • @MattMcNabb : gcc is not explicitly mentioned in the question, and the question is about whether the behaviour is true of *all* compilers. In general the the effect should at least the same as `#define FOO`. – Clifford Apr 01 '15 at 12:53
  • @whjm The question is about the compiler commandline switch `-DFOO` . The C standard does not define compiler commandline switches. Based on results reported in this thread, it seems many compilers implement `-DFOO` as `#define FOO 1` . – M.M Apr 01 '15 at 12:56
  • @Clifford OK, I have rephrased my comment. It would make sense (to me) if they implemented `-DFOO` as `#define FOO`, but it seems they don't, and the C standard does not require them to. – M.M Apr 01 '15 at 12:57
  • @MattMcNabb : It would be interesting to check whether `#define FOO` has the effect that `FOO == 1` in the compiler in question - then the behaviour would be the same - i.e. it would be consistent for `#define FOO` to be equivalent to `#define FOO=1`. Either way, my advice not to rely on this stands. – Clifford Apr 01 '15 at 13:04
  • @Clifford: Just tried and for `#define FOO`, both gcc (4.9.1) and clang (OS X 10.10) would not expand `FOO` to `1`. That's to say `#define FOO` is not the same as `#define FOO 1`. – pynexj Apr 01 '15 at 14:41
  • @whjm : All the more reason not to advocate any assumption about the command line definition and its ability to test for `==1` as it is inconsistent with the preprocessor semantics and either may be used. – Clifford Apr 01 '15 at 16:21