5.5. Data Initialization and the DATA Statement

An entity can be initialized in a type declaration statement. When an initialization expression appears in a declaration for an object that does not have the PARAMETER attribute, the object (which is a variable) is assigned the specified initial value. This object is a variable with explicit initialization. Alternatively, explicit initialization can be specified in a DATA statement unless the variable is a derived type or type alias for which default initialization has been specified.

Note: The Fortran standard has declared that the placement of DATA statements amongst executable statements is obsolescent.

The same rules apply to the assignment of the initial value when an assignment statement is executed. For example, if the variable is of type real but the value is an integer value, the variable will be assigned the real equivalent of the integer value. If the kind of the variable is different from the kind of the value, the value will be converted to the kind of the variable. Array constructors and broadcast values can be used to initialize arrays, and structure constructors can be used to initialize variables of user-defined type. The format of a type declaration statement that provides an initial value for a variable is as follows:

type [,  attribute_list ] :: object_name [ (array_spec) ] [ * char_length ]
    =  initialization_expr

Subject to the rules governing combinations of these attributes, attribute_list can contain the following:

DIMENSION
POINTER
PRIVATE
PROTECTED (F2003)
PUBLIC
SAVE
STATIC (EXTENSION)
TARGET
VOLATILE (F2003)

For example:

INTEGER :: I = 0

The PARAMETER attribute can also appear in the attribute_list, but in this case, the object is declared to be a named constant.

The value associated with the name cannot be changed during the execution of the program. For example, PI or E can be associated with the familiar mathematical constants to provide more convenient access to these values. Named constants are also used to assign names to values (such as a sales tax rate) that could change at some later time. When a change is necessary, it can be made at one place in the program rather than every place where the value is used. The program can be recompiled to effect the change.

An array name that appears in a declaration statement that contains an initialization_expr must have its dimensionality declared in the same statement or a previous statement.

Initialization of a variable in a type declaration statement or any part of a variable in a DATA statement implies that the variable has the SAVE attribute unless the variable is in a named common block. The automatically acquired SAVE attribute may be reaffirmed by the appearance of SAVE as an attribute in its type declaration statement or by inclusion of the variable name in a separate SAVE statement.

The DATA statement is defined as follows:

Table 5-12.

 

data_stmt

is

DATA data_stmt_set [ [, ] data_stmt_set ]...

 

data_stmt_set

is

data_stmt_object_list / data_stmt_value_list / [ [, ] data_stmt_object_list / data_stmt_value_list / ] ...

 

data_stmt_object

is

variable

 

 

or

data_implied_do

 

data_stmt_value

is

[ data_stmt_repeat * ] data_stmt_constant

 

data_stmt_constant

is

scalar_constant

 

 

or

scalar_constant_subobject

 

 

or

signed_int_literal_constant

 

 

or

signed_real_literal_constant

 

 

or

structure_constructor

 

 

or

boz_literal_constant

 

 

or

NULL()

EXT

 

or

typeless_constant

 

data_stmt_repeat

is

scalar_int_constant

 

 

or

scalar_int_constant_subobject

 

data_implied_do

is

(data_i_do_object_list, data_i_do_variable= scalar_int_expr, scalar_int_expr [ , scalar_int_expr ])

 

data_i_do_object

is

array_element

 

 

or

scalar_structure_component

 

 

or

data_implied_do

 

data_i_do_variable

is

scalar_int_variable

 

typeless_constant

is

hexadecimal_typeless_constant

 

 

or

binary_typeless_constant

EXT

or

octal_typeless_constant

 

octal_typeless_constant

is

O" digit [ digit... ] "

 

 

or

O' digit [ digit... ] '

EXT

 

or

" digit [ digit... ] "O

EXT

 

or

' digit [ digit... ] 'O

EXT

or

digit [ digit... ] B

 

hexadecimal_typeless_constant

is

Z'hex_digit [ hex_digit... ] '

 

 

or

Z"hex_digit [ hex_digit... ] "

EXT

or

X'hex_digit [ hex_digit... ] '

EXT

 

or

X" hex_digit [ hex_digit... ] "

EXT

 

or

'hex_digit [ hex_digit... ] 'X

EXT

 

or

"hex_digit [ hex_digit... ] "X

 

binary_typeless_constant

is

B'bin_digit [ bin_digit... ] '

 

 

or

B"bin_digit [ bin_digit... ] "

The following notes pertain to the preceding format:

Note that a constant value cannot be an array constructor. An array can be initialized by using the array name as the data_stmt_object and supplying values for all the elements of the array in array element order.

Note: The Fortran standard does not specify the typeless_constant. If an object is of type character or logical, the constant used for initialization must be of the same type.

If an object is of type real or complex, the corresponding constant must be of type integer, real, or complex. The Cray Fortran Compiler permits a default real object to be initialized with a BOZ, typeless, or character (used as Hollerith) constant. No conversion of the BOZ value, typeless value, or character constant takes place.

The Cray Fortran Compiler permits an integer object to be initialized with a BOZ, typeless, or character (used as Hollerith) constant in a type declaration statement. The Cray Fortran Compiler also allows an integer object to be initialized with a typeless or character (used as Hollerith) constant in a DATA statement.

Note: The Fortran standard does not specify typeless or character (used as Hollerith) constants in initializations, nor does it allow BOZ constants to be used in type declaration statement initializations.

If an object is of derived type or of a type alias that describes a derived type, the corresponding structure constructor must be of the same type.

The value of the constant, structure constructor (in a DATA statement), or initialization expression (in a type declaration statement) must be such that its value could be assigned to the corresponding variable using an intrinsic assignment statement. The variable becomes initially defined with the value of the constant.

A variable, or the same part of a variable, must not be initialized more than once in an executable program.

Note: If a variable is initialized more than once in a program, the order of initialization is not guaranteed. The compiler cannot enforce and does not adhere to an order for initialization when multiple initializations appear in source code. The load order can also affect the value of a variable that is initialized multiple times, which means that the final value can vary from loader to loader.

An object declared to be of a type that has default initialization cannot be specified in a DATA statement. This object can be initialized in a type specification statement. The initialization in the type specification statement overrides the default initialization.

The following items cannot be initialized:

Note: The Fortran standard does not allow initialization of objects in named common blocks except from within a BLOCKDATA program unit. The Fortran standard does not allow initialization of objects in a blank common block.

For an object being initialized, any subscript, section subscript, substring starting point, or substring ending point must be an integer initialization expression.

Each component of a structure constructor used for initialization must be an initialization expression.

If the variable being initialized has the POINTER attribute, then data_stmt_constant must be NULL(). The pointer has an initial association status of disassociated.

A variable that appears in a DATA statement and is thereby declared and typed implicitly can appear in a subsequent type declaration statement only if that declaration confirms the implicit declaration. An array name, array section, or array element appearing in a DATA statement must have had its array properties established previously.

If a DATA statement constant value is a named constant or a structure constructor, the named constant, derived type, or type alias must have been declared previously in the scoping unit or must have been made accessible by USE or HOST association.

An array element or structure component that appears in a DATA statement must not have a constant parent.

The DATA statement repeat factor value must be positive or zero. If it is a named constant, the value must have been specified in a prior statement in the scoping unit that contains the DATA statement or must have been made accessible by use or host association.

In a scalar_constant_subobject that is a data_stmt_repeat, any subscript must be an initialization expression.

In a scalar_constant_subobject that is a data_stmt_constant, any subscript, substring starting point, or substring ending point must be an initialization expression.

A subscript in an array element of an implied-DO list must contain as operands only constants or DO variables of the containing implied-DO s.

The scalar integer loop control expressions in an implied-DO must contain as operands only constants or DO variables of the containing implied-DO s. Each operation must be an intrinsic operation.

The data object list is expanded to form a sequence of scalar variables. An array or array section is equivalent to the sequence of its array elements in array element order. A data_implied_do is expanded to form a sequence of array elements and structure components, under the control of the implied-DO variable, as in the DO construct. A zero-sized array or an implied-DO with an iteration count of 0 contributes no variables to the expanded list, but a character variable declared to have zero length does contribute a variable to the list.

The data value list is expanded to form a sequence of scalar values. Each value must be a constant or constant expression (structure constructor). If a value is represented by a named constant, the named constant must be specified prior to the DATA statement. A DATA statement repeat factor indicates the number of times the following constant value is to be included in the sequence. If the repeat factor is 0, the following value is ignored.

If a data_stmt_constant is a boz_literal_constant, the corresponding object must be of type integer. A data_stmt_constant that is a boz_literal_constant is treated as if the constant were an int_literal_constant with a kind_param that specified the representation method with the largest decimal exponent range supported.

Scalar variables and values of the expanded sequence must be in one-to-one correspondence. Each value specifies the initial value for the corresponding variable. The lengths of the two expanded sequences must be the same.

Note: If the last item in the data_object_list is an array name, the value list can contain fewer values than the number of elements in the array. Any element that is not assigned a value is undefined.

The following examples show type declaration statement initializations:

CHARACTER(LEN = 10) :: NAME = "JOHN DOE"
INTEGER, DIMENSION(0:9) :: METERS = (/ (0, I = 1, 10) /)
TYPE(PERSON) :: ME = PERSON(21, "JOHN SMITH"), &
   YOU = PERSON(35, "FRED BROWN")
INTEGER :: BIRD(3) = 1
REAL :: SKEW(100,100) = RESHAPE ( (/((1.0, K = 1, J-1), &
       (0.0, K = J, 100), J = 1, 100)/), (/ 100, 100 /) )

The following are examples of DATA statement initializations:

CHARACTER*10 NAME
INTEGER METERS(0:9)
DATA NAME /"JOHN DOE"/, METERS /10*0/

TYPE(PERSON) ME, YOU
DATA ME /PERSON(21, "JOHN SMITH")/
DATA YOU%AGE, YOU%NAME /35, "FRED BROWN"/
INTEGER BIRD(3)
DATA BIRD /3*1/
REAL SKEW(100, 100)
DATA ((SKEW (K, J), K = 1, J-1), J = 1, 100) /4950 * 1.0/
DATA ((SKEW (K, J), K = J, 100), J = 1, 100) /5050 * 0.0/

In both forms, the character variable NAME is initialized with the value JOHN DOE with padding on the right because the length of the constant is less than the length of the variable. All ten elements of the integer array METERS are initialized to 0; an array constructor is used in the type declaration statement form; a repeat factor is used for the DATA statement form. ME and YOU are structures declared using the user-defined type PERSON defined in Section 4.8. In both forms ME is initialized using a structure constructor. In the DATA statement form YOU is initialized by supplying a separate value for each component.

In the type declaration statement form, the value 1 is broadcast to all 3 elements of BIRD. In the DATA statement form, a value must be supplied for each element of BIRD.

In both forms, the two-dimensional array SKEW is initialized so that the lower triangle is 0 and the strict upper triangle is 1. The RESHAPE(3i) intrinsic function is required in the first form because SKEW is of rank 2. Repeat factors are used in the second form.

5.5.1. PARAMETER Attribute and Statement

A constant can be given a name in a type declaration statement with the PARAMETER attribute or in a PARAMETER statement. The following is a format for a type declaration statement with a PARAMETER attribute:

type, PARAMETER [ , attribute_list ] :: name  = initialization_expression

Subject to the rules governing combinations of these attributes, attribute_list can contain the following:

DIMENSION
PRIVATE
PUBLIC

The initialization_expression must be present.

More than one named constant can be specified in a single type declaration statement; see the examples in this section.

The named constant becomes defined with the value determined from the initialization expression in accordance with the rules for intrinsic assignment. Any named constant that appears in the initialization expression must meet one of the following conditions:

If the named constant is an array, it must have its array properties declared in this statement or in a previous statement in the same scoping unit.

The PARAMETER statement also provides a means of defining a named constant. Its format is defined as follows:

Table 5-13.

 

parameter_stmt

is

PARAMETER ( named_constant_def_list)

 

named_constant_def

is

named_constant = initialization_expr

The PARAMETER statement also confers the PARAMETER attribute. It is subject to the same rules and restrictions as the PARAMETER attribute.

The PARAMETER attribute must not be specified for dummy arguments, pointers, allocatable objects, functions, or objects in a common block.

A named constant that appears in a PARAMETER statement and is thereby declared and typed implicitly may appear in a subsequent type declaration statement only if that declaration confirms the implicit declaration.

A named array constant appearing in a PARAMETER statement must have had its array properties established previously.

A named constant must not appear in a format specification because of a possible ambiguity.

The following examples show entity-oriented declarations:

INTEGER, PARAMETER :: STATES = 50
INTEGER, PARAMETER :: M = MOD(28, 3),  &
       NUMBER_OF_SENATORS = 2 * STATES

The following examples show attribute-oriented declarations:

INTEGER STATES, M, NUMBER_OF_SENATORS
PARAMETER(STATES = 50)
PARAMETER(M = MOD(28, 3),  &
      NUMBER_OF_SENATORS = 2 * STATES)