enum syntax >>-#pragma--enum--(--+-1--------------+--)--------------------->< +-2--------------+ +-4--------------+ +-int------------+ +-small----------+ +-pop------------+ +-system_default-+ '-user_default---'
enum 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
The value ranges that can be accepted by the enum settings are shown below:
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 |
Examples
The examples below show various uses of the #pragma enum and compiler options:
#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 */
#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 )
#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.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.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.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.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.