When are enum values resolved? In other words, is the following code snippet standard-compliant?
enum{
A,
B,
MAX
}
#if MAX > 42
# error "Woah! MAX is a lot!"
#endif
The preprocessor doesn't have anything to do with enums. But your example compiles without error, so what's going on with the #if MAX > 42
directive?
Whenever the preprocessor is handling a conditional directive, any identifiers that are not defined as macros are treated as 0. So assuming that MAX
isn't defined elsewhere as a macro, your snippet of code is equivalent to:
enum{
A,
B,
MAX
}
#if 0 > 42
# error "Woah! MAX is a lot!"
#endif
From C99 6.10.1/3 "Conditional inclusion":
... After all replacements due to macro expansion and the defined unary operator have been performed, all remaining identifiers are replaced with the pp-number 0, and then each preprocessing token is converted into a token. ...
The same wording is in the C89/C90 standard.
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