8.2.4 Enumeration types

By default all enumerations are stored as a longword (4 bytes), which is equivalent to specifying the {$Z4}, {$PACKENUM 4} or {$PACKENUM DEFAULT} switches.

This default behavior can be changed by compiler switches, and by the compiler mode.

In the tp compiler mode, or while the {$Z1} or {$PACKENUM 1} switches are in effect, the storage space used is shown in table (8.1).


Table 8.1: Enumeration storage for tp mode

# Of Elements in Enum. Storage space used


0..255 byte (1 byte)
256..65535 word (2 bytes)
¿ 65535 longword (4 bytes)



When the {$Z2} or {$PACKENUM 2} switches are in effect, the value is stored in 2 bytes (a word), if the enumeration has less or equal than 65535 elements. If there are more elements, the enumeration value is stored as a 4 byte value (a longword).

It is important to note that the number of elements is determined by the highest value of the enumeration. If the enumeration has assigned values, then the ”number of elements” is actually the highest value in the enumeration+1.

program Project1;
{$PACKENUM 2}
type
  TEnum = (a, b, c = $FFFF + 1);
begin
  writeln(sizeof(TEnum)); // 4
end.

will print 4 because the number of elements is $FFFF+2