7

While looking through some c header files (specifically stdarg.h), I noticed a very peculiar line:

#if !defined(__STRICT_ANSI__) || __STDC_VERSION__ + 0 >= 199900L

The strange part is the + 0. Zero is the additive identity; it's one of the various math has of writing noop.

What purpose does adding zero have in the above preprocessor statement? I know that there's all sorts of weird preprocessor magic out there, but this just seems ridiculous.

Community
  • 1
  • 1
ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79

1 Answers1

12

That avoids a preprocessor syntax error if __STDC_VERSION__ is defined as an empty token (e.g. with #define __STDC_VERSION__).

(Thanks to Jens Gustedt for pointing out that the first version of my answer was wrong.)

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 1
    Not entirely correct. The rules for preprocessor evaluation have unknown identifier tokens evaluated to 0, so it would be fine if it weren't defined. The case where this matters is when it would be defined, but to the empty token. – Jens Gustedt Dec 07 '13 at 23:43
  • @JensGustedt: You are right, I just tried to verify my answer in some test code and noticed that it is not correct. I hope that it is correct now. – Martin R Dec 07 '13 at 23:45
  • Twisted. I guess the + sign is just interpreted as the unary operator if the token is blank. – ApproachingDarknessFish Dec 07 '13 at 23:54
  • @ValekHalfHeart: Yes, exactly. – Martin R Dec 07 '13 at 23:56