avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Can enumerator values be used in a #if preprocessordi


From: Weddington, Eric
Subject: Re: [avr-gcc-list] Can enumerator values be used in a #if preprocessordirective?
Date: Mon, 29 Aug 2011 12:42:48 -0600

Hi Bob,

The preprocessor runs, of course, before the compiler.

So you're trying to check if an enum is greater than some value, when the 
compiler phase hasn't even evaluated the enum list and assigned values to 
"last".

For the preprocessor purposes, I would bet that somehow "last" evaluates to 0, 
so of course the condition will always be true.

You might have to change the conditional compilation to some sort of assert.

Eric

> -----Original Message-----
> From: address@hidden
> [mailto:address@hidden On
> Behalf Of Bob Paddock
> Sent: Monday, August 29, 2011 12:37 PM
> To: AVR-GCC
> Subject: [avr-gcc-list] Can enumerator values be used in a #if
> preprocessordirective?
> 
> I have the following file test.c:
> 
> #define MAX (2U)
> enum ENUM_EXAMPLE{ a, b, c, d, e, last };
> #if( last >= MAX )
> #error Last is greater than MAX.
> #endif
> 
> /*
>  * Never get as far as this main when compiling with other code to mater,
>  * need for the standalone test:
>  */
> int main( void )
> {
> }
> 
> If I do 'avr-gcc test.c' I get an a.out file and no errors.
> If I include test.c in my project I get 'error: "last" is not defined'.
> Why would it be defined in the standalone one and not defined in the
> other?
> 
> Looking in "C: A Reference Manual" 5th edition by Harbison and Steele they
> make the distinction between "preprocessor constant expressions" and
> "integral constant expressions" in section 7.11/7.11.1.  In 7.11.1
> enums are not listed as a allowable type to #if.  If that is the case
> why does the standalone compile of test.c compile?  If that is not the
> case why is 'last' not defined?
> 
> I'm confused by this inconstancy, what am I overlooking here?
> 
> The goal in my real code is to know when I've assigned more than 254
> enums in one enumerator set.
> Is there a different way to do this?
> 
> _______________________________________________
> AVR-GCC-list mailing list
> address@hidden
> https://lists.nongnu.org/mailman/listinfo/avr-gcc-list



reply via email to

[Prev in Thread] Current Thread [Next in Thread]