[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


Enumerated Types

(K&R, §A8.4) This section describes how the C compiler selects the underlying integer data type for an enumerated type. The compiler behavior is controlled primarily by the Enums Always Int option in the C/C++ Language settings panel.

The compiler uses one of two different strategies, depending on the setting of the Enums Always Int option.

If Enums Always Int is on, the underlying type is always signed int. All enumerators must be no larger than a signed int. If an enumerated constant is larger than an int, the compiler generates an error.

However, if the ANSI Strict option is off, enumerators that can be represented as an unsigned int are implicitly converted to signed int. For example:


#pragma enumsalwaysint on
#pragma ANSI_strict on
enum foo { a=0xFFFFFFFF }; // ERROR. a is 4,294,967,295:
                                                      //                too big for a signed int
#pragma ANSI_strict off
enum bar { b=0xFFFFFFFF }; // OK: b can be represented as an
                                                      //          unsigned int, but is implicitly
                                                      //          converted to a signed int (-1).

See "ANSI Strict" for additional features of that setting.

If Enums Always Int is off, the compiler chooses the integral data type that supports the largest enumerated constant. The type could be as small as a char or as large as a long int. It could even be a 64-bit long long value.

To be more precise, if Enums Always Int is off, the compiler picks one of the following:

For example:


#pragma enumsalwaysint off
enum { a=0,b=1 };         // base type: unsigned char
enum { c=0,d=-1 };        // base type: signed char
enum { e=0,f=128,g=-1 };  // base type: signed short

The compiler will use long long data types only if Enums Always Int is off and the longlong_enums pragma is on. (There is no settings panel option corresponding to the longlong_enums pragma.)

For example:


#pragma enumsalwaysint off
#pragma longlong_enums off
enum { a=0x7FFFFFFFFFFFFFFF }; // ERROR: a is too large
#pragma longlong_enums on
enum { b=0x7FFFFFFFFFFFFFFF };// OK: base type: signed long long
enum { c=0x8000000000000000 };// OK: base type: unsigned long long
enum {þd=-1,e=0x80000000 };     // OK: base type: signed long long

When the longlong_enums pragma is off and ANSI Strict is on, you cannot mix unsigned 32-bit enumerators greater than 0x7FFFFFFF and negative enumerators. If both the longlong_enums pragma and the ANSI Strict option are off, huge unsigned 32-bit enumerators are implicitly converted to signed 32-bit types.

For example:


#pragma enumsalwaysint off
#pragma longlong_enums off
#pragma ANSI_strict on
enum { a=-1,b=0xFFFFFFFF };    // error
#pragma ANSI_strict off
enum { c=-1,d=0xFFFFFFFF };    // base type: signed int (b==-1)

The Enums Always Int option corresponds to the pragma enumsalwaysint. To check whether this option is on, use __option (enumsalwaysint). By default, this option is off.

See also "enumsalwaysint", "longlong_enums", and "Checking Options."


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 17, 2000