enum

C compiler
Read syntax diagramSkip visual syntax diagramenum syntax
 
>>-#pragma--enum--(--+-1--------------+--)---------------------><
                     +-2--------------+
                     +-4--------------+
                     +-int------------+
                     +-small----------+
                     +-pop------------+
                     +-system_default-+
                     '-user_default---'
 
C++ compiler
Read syntax diagramSkip visual syntax diagramenum syntax
 
>>-#pragma--enum--(--+-1--------------+--)---------------------><
                     +-2--------------+
                     +-4--------------+
                     +-8--------------+
                     +-int------------+
                     +-intlong--------+
                     +-small----------+
                     +-pop------------+
                     +-system_default-+
                     '-user_default---'
 

Description

Specifies the number of bytes the compiler uses to represent enumerations. The pragma affects all subsequent enum definitions until the end of the compilation unit or until another #pragma enum directive is encountered. If more than one pragma is used, the most recently encountered pragma is in effect. This pragma overrides the ENUM compiler option, described on page ENUM.

Parameters

Start of change1, 2, 4, 8End of change
Start of changeSpecifies that enumerations be stored in 1, 2, 4, or 8-byte containers. The sign of the container is determined by the range of values in the enumeration, but preference is given to signed when the range permits either. The pragma enum(8) directive is only available in C++.End of change
int
Causes enumerations to be stored in the ANSI C or C++ Standard representation of an enumeration, which is 4-bytes signed. In C++ programs, the int container may become 4-bytes unsigned if a value in the enumeration exceeds 231-1, as per the ANSI C++ Standard.
Start of changeintlongEnd of change
Start of changeSpecifies that enumerations occupy 8 bytes of storage if the range of values in the enumeration exceeds the limit for int. If the range of values in the enumeration does not exceed the limit for int, the enumeration will occupy 4 bytes of storage and is represented as though enum(int) was specified. The pragma enum(intlong) directive is only available in C++End of change
small
Causes subsequent enumerations to be placed into the smallest possible container, given the values in the enumeration. The sign of the container is determined by the range of values in the enumeration, but preference is given to unsigned when the range permits either.
pop
Selects the enumeration size previously in effect, and discards the current setting.
system_default
Selects the default enumeration size, which is the small option.
user_default
Selects the enumeration size specified by the ENUM compiler option.

The value ranges that can be accepted by the enum settings are shown below:

Start of change
Table 2. Value Ranges Accepted by the enum Settings
Range of Element Values Enum Options
small (default) 1 2 4 8

(C++ only)

int intlong

(C++ only)

0 .. 127 1 byte unsigned 1 byte signed 2 bytes signed 4 bytes signed 8 bytes signed 4 bytes signed 4 bytes signed
0 .. 255 1 byte unsigned 1 byte unsigned 2 bytes signed 4 bytes signed 8 bytes signed 4 bytes signed 4 bytes signed
-128 .. 127 1 byte signed 1 byte signed 2 bytes signed 4 bytes signed 8 bytes signed 4 bytes signed 4 bytes signed
0 .. 32767 2 bytes unsigned ERROR 2 bytes signed 4 bytes signed 8 bytes signed 4 bytes signed 4 bytes signed
0 .. 65535 2 bytes unsigned ERROR 2 bytes unsigned 4 bytes signed 8 bytes signed 4 bytes signed 4 bytes signed
-32768 .. 32767 2 bytes signed ERROR 2 bytes signed 4 bytes signed 8 bytes signed 4 bytes signed 4 bytes signed
0 .. 2147483647 4 bytes unsigned ERROR ERROR 4 bytes signed 8 bytes signed 4 bytes signed 4 bytes signed
0 .. 4294967295 4 bytes unsigned ERROR ERROR 4 bytes unsigned 8 bytes signed C++ 4 bytes unsigned

C ERROR

4 bytes unsigned
-2147483648 .. 2147483647 4 bytes signed ERROR ERROR 4 bytes signed 8 bytes signed 4 bytes signed 4 bytes signed
0 .. (263 -1)

(C++ only)

8 bytes unsigned ERROR ERROR ERROR 8 bytes signed ERROR 8 bytes signed
0.. 2 64

(C++ only)

8 bytes unsigned ERROR ERROR ERROR 8 bytes unsigned ERROR 8 bytes unsigned
-2 63 .. (2 63-1)

(C++ only)

8 bytes signed ERROR ERROR ERROR 8 bytes signed ERROR 8 bytes signed
End of change

Examples

The examples below show various uses of the #pragma enum and compiler options:

  1. You cannot change the storage allocation of an enum by using #pragma enum within the declaration of an enum. The following code segment generates a warning and the second occurrence of the enum option is ignored:
    #pragma enum ( small )
       enum e_tag { a, b,
    #pragma enum ( int ) /* error: cannot be within a declaration */
       c
      } e_var;
    
    #pragma enum ( pop ) /* second pop isn't required */ 
  2. Start of changeThe range of enum constants must fall within the range of either unsigned int or int (signed int) for the C compiler. The range of enum constants must fall within the range of either unsigned long long or long long (signed long long) for the C++ compiler. For example, the following code segments contain errors when the C compiler is used, but will compile successfully when the C++ compiler is used.:
    #pragma enum ( small )
       enum e_tag { a=-1,
                    b=2147483648   /* C compiler error: larger than maximum int */
                  } e_var;
    #pragma enum ( pop )
    
    #pragma enum ( small )
       enum e_tag { a=0,
                    b=4294967296 /* C compiler error: larger than maximum int */
                  } e_var;
    #pragma enum ( pop )
    End of change
  3. One use for the pop option is to pop the enumeration size set at the end of an include file that specifies an enumeration storage different from the default in the main file. For example, the following include file, small_enum.h, declares various minimum-sized enumerations, then resets the specification at the end of the include file to the last value on the option stack:
    #ifndef small_enum_h
    #define small_enum_h 
    /*
     * File small_enum.h
     * This enum must fit within an unsigned char type
    */
    #pragma enum ( small )
       enum e_tag {a, b=255};
       enum e_tag u_char_e_var; /* occupies 1 byte of storage */
          
    /* Pop the enumeration size to whatever it was before */
    #pragma enum ( pop )
    #endif
    The following source file, int_file.c, includes small_enum.h:
    /*
     * File int_file.c
     * Defines 4 byte enums
    */
    #pragma enum ( int )
       enum testing {ONE, TWO, THREE};
       enum testing test_enum;
    
    /* various minimum-sized enums are declared */
    #include "small_enum.h"
          
    /* return to int-sized enums. small_enum.h has popped the enum size
    */
       enum sushi {CALIF_ROLL, SALMON_ROLL, TUNA, SQUID, UNI};
       enum sushi first_order = UNI;
    The enumerations test_enum and first_order both occupy 4 bytes of storage and are of type int. The variable u_char_e_var defined in small_enum.h occupies 1 byte of storage and is represented by an unsigned char data type.
  4. If the code fragment below is compiled with the ENUM = *SMALL option:
    enum e_tag {a, b, c} e_var;
    the range of enum constants is 0 through 2. This range falls within all of the ranges described in the table above. Based on priority, the compiler uses predefined type unsigned char.
  5. If the code fragment below is compiled with the ENUM = *SMALL option:
    enum e_tag {a=-129, b, c} e_var;
    the range of enum constants is -129 through -127. This range only falls within the ranges of short (signed short) and int (signed int). Because short (signed short) is smaller, it will be used to represent the enum.
  6. If you compile a file myprogram.c using the command:
    CRTBNDC MODULE(MYPROGRAM) SRCMBR(MYPROGRAM) ENUM(*SMALL)
    all enum variables within your source file will occupy the minimum amount of storage, unless #pragma enum directives override the ENUM option.
  7. If you compile a file yourfile.c that contains the following lines:
    enum testing {ONE, TWO, THREE};
    enum testing test_enum;
          
    #pragma enum ( small )
    enum sushi {CALIF_ROLL, SALMON_ROLL, TUNA, SQUID, UNI};
    enum sushi first_order = UNI;
          
    #pragma enum ( int )
    enum music {ROCK, JAZZ, NEW_WAVE, CLASSICAL};
    enum music listening_type;
    using the command:
    CRTBNDC MODULE(YOURFILE) SRCMBR(YOURFILE)  	
    the enum variables test_enum and first_order will be minimum-sized (that is, each will only occupy 1 byte of storage). The other enum variable, listening_type, will be of type int and occupy 4 bytes of storage.


[ Top of Page | Previous Page | Next Page | Contents | Index ]