Fortran 2003

ENUM/END ENUM

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 diagramEnumeration construct
 
>>-ENUM, BIND(C)-----------------------------------------------><
 
Read syntax diagramSkip visual syntax diagram>>-enumeration_block-------------------------------------------><
 
Read syntax diagramSkip visual syntax diagram>>-ENDENUM-----------------------------------------------------><
 
Read syntax diagramSkip visual syntax diagramenumeration_block
 
                       .-,-------------------------------------.
                       V                                       |
>>-ENUMERATOR--+----+----named_constant--+-------------------+-+-><
               '-::-'                    '-=--scalar_int_exp-'
 

If you want to specify an enumerator with a scalar_initialization_expression, you must also specify a ::.

Rules

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

IBM Extension

You can only use a logical operator if you compile with -qintlog.

End of IBM Extension

If you do not specify a scalar integer initialization expression and the enumerator is first in the definition body of the type, the value of the enumerator is 0.

If you do not specify a scalar integer initialization expression and the enumerator is after another enumerator in the enumeration definition, 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.

Example of an ENUM statement
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 initialization expression.

The :: is optional in an enumerator definition when scalar integer initialization expressions are not used to initialize any of the enumerators in the list of enumerators being declared:

Related information

End of Fortran 2003