I'm reading a book about C programming "Advanced Programming In The Unix Environment" and in the "UNIX Standardization and Implementations" chapter it shows a program that prints values for symbols in sysconf and pathconf, the code in this program has several lines that looks like this:
#ifdef ARG_MAX
printf ("ARG_MAX defined to be %ld\n", (long) ARG_MAX + 0);
#else
printf ("no symbol for ARG_MAX\n");
#endif
Whenever a defined constant is used as an argument for printf
it's always followed by + 0
, it doesn't seem to be doing anything because I tried to remove it and nothing happened. What's the point of adding a zero?
The empty #define
mentioned by melpomene in a comment is why the + 0
trick is used.
If some header has #define ARG_MAX
(with no value) — an improbable but not impossible state of affairs — the code will still compile, though whether the output is useful is a separate discussion. The blank replacement for ARG_MAX
leaves (long) + 0
which is still valid. If, as you'd expect, the definition is an appropriately protected expression (probably within parentheses), adding 0
doesn't change the value.
In the ordinary course of events, when the preprocessor evaluates a conditional expression, any identifiers left over after expanding macros are mapped to zero (ISO/IEC 9899:2011 §6.10.1 Conditional inclusion):
¶4 … 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, and then each preprocessing token is converted into a token. …
Although that's not immediately relevant to this question, it is why you sometimes see a conditional expression such as:
#if ARG_MAX + 0 > 131072
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With