Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are enum values resolved in preprocess time or in compile time?

Tags:

c

c11

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
like image 695
Vorac Avatar asked Apr 19 '13 08:04

Vorac


1 Answers

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.

like image 175
Michael Burr Avatar answered Oct 18 '22 23:10

Michael Burr