Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

Enumerations and Enumerators

An enumeration defines the name of a group of related values and the name of each value within the group. It takes the following form:

ENUM, BIND(C)

    ENUMERATOR [ :: ] c1 [= expr][, c2 [= expr]]...

    [ENUMERATOR [ :: ] c3 [= expr][, c4 [= expr]]...]...

END ENUM

c1,c2,c3,c4

Is the name of the enumerator being defined.

expr

Is an optional scalar integer constant expression specifying the value for the enumerator.

If = appears in an enumerator, a double-colon separator must appear before the enumerator or list of enumerators.

The compiler ensures that the integer kind declared for the enumerator is compatible with the integer type used by the corresponding C enumeration. The processor uses the same representation for the types declared by all C enumeration specifiers that specify the same values in the same order.

An enumerator is treated as if it were explicitly declared with the PARAMETER attribute.

The order in which the enumerators appear in an enumerator definition is significant.

If you do not explicitly assign each enumerator a value by specifying an expr, the compiler assigns a value according to the following rules:

  • If the enumerator is the first enumerator in the enumerator definition, the enumerator has the value 0.

  • If the enumerator is not the first enumerator in the enumerator definition, its value is the result of adding one to the value of the immediately preceding enumerator in the enumerator definition.

You can define the enumerators in multiple ENUMERATOR statements or in one ENUMERATOR statement. The order in which the enumerators are declared in an enumeration definition is significant, but the number of ENUMERATOR statements is not.

Examples

The following example shows an enumeration definition:

ENUM, BIND(C)
  ENUMERATOR ORANGE
  ENUMERATOR :: RED = 5, BLUE = 7
  ENUMERATOR GREEN
END ENUM

The kind type parameter for this enumeration is processor dependent, but the processor must select a kind sufficient to represent the values of the enumerators.

The order of the enumerators is significant if the values are not assigned explicitly. In the above example, the value of ORANGE becomes defined as a named constant with the value zero and the value of GREEN becomes defined as a named constant with the value 8. Note that if RED was the enumerator preceding GREEN, the value of GREEN would be 6 rather than 8.

The following declaration may be considered to be equivalent to the above enumeration definition:

INTEGER(SELECTED_INT_KIND(4)), PARAMETER :: ORANGE = 0, RED = 5, BLUE = 7, GREEN = 8

An entity of the same kind type parameter value can be declared using the intrinsic function KIND with one of the enumerators as its argument, for example:

INTEGER(KIND(BLUE)) :: X