1.19 -fshort-enums, -fno-short-enums

Allows the compiler to set the size of an enumeration type to the smallest data type that can hold all enumerator values.

The -fshort-enums option can improve memory usage, but might reduce performance because narrow memory accesses can be less efficient than full register-width accesses.

Note

All linked objects, including libraries, must make the same choice. It is not possible to link an object file compiled with -fshort-enums, with another object file that is compiled without -fshort-enums.

Note

The -fshort-enums option is not supported for AArch64. The Procedure Call Standard for the ARM® 64-bit Architecture states that the size of enumeration types must be at least 32 bits.

Default

The default is -fno-short-enums. That is, the size of an enumeration type is at least 32 bits regardless of the size of the enumerator values.

Example

This example shows the size of four different enumeration types: 8-bit, 16-bit, 32-bit, and 64-bit integers.
#include <stdio.h>

// Largest value is 8-bit integer
enum int8Enum  {int8Val1 =0x01, int8Val2 =0x02, int8Val3 =0xF1 }; 

// Largest value is 16-bit integer              
enum int16Enum {int16Val1=0x01, int16Val2=0x02, int16Val3=0xFFF1 };             

// Largest value is 32-bit integer
enum int32Enum {int32Val1=0x01, int32Val2=0x02, int32Val3=0xFFFFFFF1 }; 

// Largest value is 64-bit integer        
enum int64Enum {int64Val1=0x01, int64Val2=0x02, int64Val3=0xFFFFFFFFFFFFFFF1 }; 

int main(void)
{
  printf("size of int8Enum  is %zd\n", sizeof (enum int8Enum));
  printf("size of int16Enum is %zd\n", sizeof (enum int16Enum));
  printf("size of int32Enum is %zd\n", sizeof (enum int32Enum));
  printf("size of int64Enum is %zd\n", sizeof (enum int64Enum));
}
When compiled without the -fshort-enums option, all enumeration types are 32 bits (4 bytes) except for int64Enum which requires 64 bits (8 bytes):
armclang --target=arm-arm-none-eabi -march=armv8-a enum_test.cpp
				
size of int8Enum  is 4
size of int16Enum is 4
size of int32Enum is 4
size of int64Enum is 8
When compiled with the -fshort-enums option, each enumeration type has the smallest size possible to hold the largest enumerator value:
armclang -fshort-enums --target=arm-arm-none-eabi -march=armv8-a enum_test.cpp
                
size of int8Enum  is 1
size of int16Enum is 2
size of int32Enum is 4
size of int64Enum is 8

Note

ISO C restricts enumerator values to the range of int. By default armclang does not issue warnings about enumerator values that are too large, but with -Wpedantic a warning is displayed.
Related information
Procedure Call Standard for the ARM 64-bit Architecture (AArch64)
Non-ConfidentialPDF file icon PDF versionARM DUI0774E
Copyright © 2014-2016 ARM. All rights reserved.