Type Declarations

Statement: Explicitly specifies the properties of data objects or functions.

Syntax

A type declaration statement has the general form:

type[ [, att] ... :: ] v[/c-list/][, v[/c-list/]] ...

type

Is one of the following data type specifiers:

BYTE

INTEGER[([KIND=]k)]

REAL[([KIND=]k)]

DOUBLE PRECISION

COMPLEX[([KIND=]k)]

DOUBLE COMPLEX

CHARACTER[([KIND=]k)]

LOGICAL[([KIND=]k)]

TYPE (derived-type-name)

In the optional kind selector "([KIND=]k)", k is the kind parameter. It must be an acceptable kind parameter for that data type. If the kind selector is not present, entities declared are of default type.

Kind parameters for intrinsic numeric and logical data types can also be specified using the *n format, where n is the length (in bytes) of the entity; for example, INTEGER*4.

See each data type for further information on that type.

att

Is one of the following attribute specifiers:

You can also declare any attribute separately as a statement.

att

Is the name of a data object or function. It can optionally be followed by:

  • An array specification, if the object is an array.

    In a function declaration, an array must be a deferred-shape array if it has the POINTER attribute; otherwise, it must be an explicit-shape array.

  • A character length, if the object is of type character.

  • An initialization expression preceded by an = or, for pointer objects, => NULL( ).

A function name must be the name of an intrinsic function, external function, function dummy procedure, or statement function.

c-list

Is a list of constants, as in a DATA statement. If v has the PARAMETER attribute, the c-list cannot be present.

The c-list cannot specify more than one value unless it initializes an array. When initializing an array, the c-list must contain a value for every element in the array.

Description

Type declaration statements must precede all executable statements.

In most cases, a type declaration statement overrides (or confirms) the implicit type of an entity. However, a variable that appears in a DATA statement and is typed implicitly can appear in a subsequent type declaration only if that declaration confirms the implicit typing.

The double colon separator (::) is required only if the declaration contains an attribute specifier or initialization; otherwise it is optional.

If att appears, c-list cannot be specified; for example:

INTEGER I /2/ ! Valid

INTEGER, SAVE :: I /2/ ! Invalid

The same attribute must not appear more than once in a given type declaration statement, and an entity cannot be given the same attribute more than once in a scoping unit.

If the PARAMETER attribute is specified, the declaration must contain an initialization expression.

If => NULL( ) is specified for a pointer, its initial association status is disassociated.

A variable (or variable subobject) can only be initialized once in an executable program.

The INTENT, VALUE, and OPTIONAL attributes can be specified only for dummy arguments.

The VALUE attribute must not be specified for a dummy procedure.

If a declaration contains an initialization expression, but no PARAMETER attribute is specified, the object is a variable whose value is initially defined. The object becomes defined with the value determined from the initialization expression according to the rules of intrinsic assignment.

The presence of initialization implies that the name of the object is saved, except for objects in named common blocks or objects with the PARAMETER attribute.

The following objects cannot be initialized in a type declaration statement:

An object can have more than one attribute. The following table lists the compatible attributes:

Compatible Attributes

Attribute

Compatible with:

ALLOCATABLE

AUTOMATIC, ASYNCHRONOUS, DIMENSION 1, PRIVATE, PROTECTED, PUBLIC, SAVE, STATIC, TARGET, VOLATILE

ASYNCHRONOUS

ALLOCATABLE, AUTOMATIC, BIND, DIMENSION, INTENT, OPTIONAL, POINTER, PROTECTED, PUBLIC, SAVE, STATIC, TARGET, VALUE, VOLATILE

AUTOMATIC

ALLOCATABLE, ASYNCHRONOUS, BIND, DIMENSION, POINTER, PROTECTED, TARGET, VOLATILE

BIND

ASYNCHRONOUS, AUTOMATIC, DIMENSION, EXTERNAL, PRIVATE, PROTECTED, PUBLIC, SAVE, STATIC, TARGET, VOLATILE

DIMENSION

ALLOCATABLE, ASYNCHRONOUS, AUTOMATIC, BIND, INTENT, OPTIONAL, PARAMETER, POINTER, PRIVATE, PROTECTED, PUBLIC, SAVE, STATIC, TARGET, VOLATILE

EXTERNAL

BIND, OPTIONAL, PRIVATE, PUBLIC

INTENT

ASYNCHRONOUS, DIMENSION, OPTIONAL, TARGET, VOLATILE

INTRINSIC

PRIVATE, PUBLIC

OPTIONAL

ASYNCHRONOUS, DIMENSION, EXTERNAL, INTENT, POINTER, TARGET, VALUE, VOLATILE

PARAMETER

DIMENSION, PRIVATE, PUBLIC

POINTER

ASYNCHRONOUS, AUTOMATIC, DIMENSION 1, OPTIONAL, PRIVATE, PROTECTED, PUBLIC, SAVE, STATIC, VOLATILE

PRIVATE

ASYNCHRONOUS, ALLOCATABLE, BIND, DIMENSION, EXTERNAL, INTRINSIC, PARAMETER, POINTER, PROTECTED, SAVE, STATIC, TARGET, VOLATILE

PROTECTED

ALLOCATABLE, ASYNCHRONOUS, BIND, DIMENSION, POINTER, PRIVATE, PUBLIC, SAVE, TARGET, VOLATILE

PUBLIC

ASYNCHRONOUS, ALLOCATABLE, BIND, DIMENSION, EXTERNAL, INTRINSIC, PARAMETER, POINTER, PROTECTED, SAVE, STATIC, TARGET, VOLATILE

SAVE

ALLOCATABLE, ASYNCHRONOUS, BIND, DIMENSION, POINTER, PRIVATE, PROTECTED, PUBLIC, STATIC, TARGET, VOLATILE

STATIC

ALLOCATABLE, ASYNCHRONOUS, BIND, DIMENSION, POINTER, PRIVATE, PROTECTED, PUBLIC, SAVE, TARGET, VOLATILE

TARGET

ALLOCATABLE, ASYNCHRONOUS, AUTOMATIC, BIND, DIMENSION, INTENT, OPTIONAL, PRIVATE, PROTECTED, PUBLIC, SAVE, STATIC, VALUE, VOLATILE

VALUE

ASYNCHRONOUS, INTENT (IN only), OPTIONAL, TARGET

VOLATILE

ALLOCATABLE, ASYNCHRONOUS, AUTOMATIC, BIND, DIMENSION, INTENT, OPTIONAL, POINTER, PRIVATE, PROTECTED, PUBLIC, SAVE, STATIC, TARGET

1With deferred shape

Example

The following show valid type declaration statements:

DOUBLE PRECISION B(6)

INTEGER(KIND=2) I

REAL(KIND=4) X, Y

REAL(4) X, Y

LOGICAL, DIMENSION(10,10) :: ARRAY_A, ARRAY_B

INTEGER, PARAMETER :: SMALLEST = SELECTED_REAL_KIND(6, 70)

REAL(KIND (0.0)) M

COMPLEX(KIND=8) :: D

TYPE(EMPLOYEE) :: MANAGER

REAL, INTRINSIC :: COS

CHARACTER(15) PROMPT

CHARACTER*12, SAVE :: HELLO_MSG

INTEGER COUNT, MATRIX(4,4), SUM

LOGICAL*2 SWITCH

REAL :: X = 2.0

TYPE (NUM), POINTER :: FIRST => NULL()

The following shows more examples:

REAL a (10)

LOGICAL, DIMENSION (5, 5) :: mask1, mask2

COMPLEX :: cube_root = (-0.5, 0.867)

INTEGER, PARAMETER :: short = SELECTED_INT_KIND (4)

REAL (KIND (0.0D0)) a1

REAL (KIND = 2) b

COMPLEX (KIND = KIND (0.0D0)) :: c

INTEGER (short) k ! Range at least -9999 to 9999

TYPE (member) :: george

See Also