ENUM/END ENUM (Fortran 2003)

Purpose

You can specify an ENUM statement to define and group a set of named integer constants. The named integer constants in an ENUM statement are called enumerators.

Syntax

To define an enumerator, you must use an enumeration construct:
Read syntax diagramSkip visual syntax diagram
Enumeration construct

>>-ENUM, BIND(C)-----------------------------------------------><

Read syntax diagramSkip visual syntax diagram
>>-enumeration_block-------------------------------------------><

Read syntax diagramSkip visual syntax diagram
>>-ENDENUM-----------------------------------------------------><

Read syntax diagramSkip visual syntax diagram
enumeration_block

                       .-,-------------------------------------.   
                       V                                       |   
>>-ENUMERATOR--+----+----named_constant--+-------------------+-+-><
               '-::-'                    '-=--scalar_int_exp-'     

If you want to specify an enumerator with a scalar_int_exp, you must also specify a double colon seperator (::).

Rules

If you specify a scalar integer constant expression, the value of the enumerator is the result of the scalar integer constant expression.

You can use a scalar logical constant expression only if you compile with -qintlog.

If you do not specify a scalar integer constant expression and the enumerator is first in the enumeration_block, the value of the enumerator is 0.

If you do not specify a scalar integer constant expression and the enumerator is after another enumerator in the enumeration_block, the value is one greater than the value of the preceding enumerator.

You can set the kind type parameter of an enumerator using the -qenum option. If you do not specify -qenum, the default kind for an enumerator is 4.

Examples

The following example uses the ENUM statement in different ways to define enumerators.
enum, bind(c)

   enumerator :: red =1, blue, black =5
   enumerator yellow
   enumerator gold, silver, bronze
   enumerator :: purple
   enumerator :: pink, lavender

endenum

The values of these enumerators are: red = 1, blue = 2, black = 5, yellow = 6 , gold = 7, silver = 8, bronze = 9, purple = 10, pink = 11, lavender = 12.

If you supply an initial value for an enumerator, then a :: is required in the ENUMERATOR statement. The red and black enumerators in the list are initialized with a scalar integer constant expression.

The :: is optional in an enumerator definition when scalar integer constant expressions are not used to initialize any of the enumerators in the list of enumerators being declared:
  • In the second and third enumerator definitions, the :: is not necessary as yellow, gold, silver, and bronze are not initialized with a scalar integer constant expression.
  • The fourth and fifth enumerator definitions show that :: can be used even when purple is not initialized with a scalar integer constant expression.

Related information