# 5.5 DATA Statement

The DATA statement assigns initial values to variables before program execution. It takes the following form:

  DATA var-list /c-list/[[,] var-list /c-list/]...
  var-list
  Is a list of variables or implied-do lists, separated by commas.

  Subscript expressions and expressions in substring references must be initialization expressions.

  An implied-do list in a DATA statement takes the following form:

    (do-list, var = expr1, expr2 [, expr3])
    do-list
    Is a list of one or more array elements, substrings, scalar structure components, or implied-do lists, separated by commas. Any array elements or scalar structure components must not have a constant parent.
    var
    Is the name of a scalar integer variable (the implied-do variable).
    expr
    Are scalar integer expressions. The expressions can contain variables of other implied-do lists that have this implied-do list within their ranges.
  c-list
  Is a list of constants (or names of constants), or for pointer objects, NULL( ); constants must be separated by commas. If the constant is a structure constructor, each component must be an initialization expression. If the constant is in binary, octal, or hexadecimal form, the corresponding object must be of type integer.

  A constant can be specified in the form r*constant, where r is a repeat specification. It is a nonnegative scalar integer constant (with no kind parameter). If it is a named constant, it must have been declared previously in the scoping unit or made accessible through use or host association. If r is omitted, it is assumed to be 1.

Rules and Behavior

A variable can be initialized only once in an executable program. 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 number of constants in c-list must equal the number of variables in var-list. The constants are assigned to the variables in the order in which they appear (from left to right).

The following objects cannot be initialized in a DATA statement:

- A dummy argument
- A function
- A function result
- An automatic object
- An allocatable array
- A variable that is accessible by use or host association
- A variable in a named common block (unless the DATA statement is in a block data program unit)
- A variable in blank common

Except for variables in named COMMON blocks, a named variable has the SAVE attribute if any part of it is initialized in a DATA statement. You can confirm this property by specifying the variable in a SAVE statement or a type declaration statement containing the SAVE attribute.

When an unsubscripted array name appears in a DATA statement, values are assigned to every element of that array in the order of subscript progression. The associated constant list must contain enough values to fill the array.

Array element values can be initialized in three ways: by name, by element, or by an implied-do list (interpreted in the same way as a DO construct).

The following conversion rules and restrictions apply to variable and constant list items:

- If the constant and the variable are both of numeric type, the following conversion occurs:
  - The constant value is converted to the data type of the variable being initialized, if necessary.
  - When a binary, octal, or hexadecimal constant is assigned to a variable or array element, the number of digits that can be assigned depends on the data type of the data item. If the constant contains fewer digits than the capacity of the variable or array element, the constant is extended on the left with zeros. If the constant contains more digits than can be stored, the constant is truncated on the left.
- If the constant and the variable are both of character type, the following conversion occurs:
  - If the length of the constant is less than the length of the variable, the rightmost character positions of the variable are initialized with blank characters.
  - If the length of the constant is greater than the length of the variable, the character constant is truncated on the right.
- If the constant is of numeric type and the variable is of character type, the following restrictions apply:
  - The character variable must have a length of one character.
  - The constant must be an integer, binary, octal, or hexadecimal constant, and must have a value in the range 0 through 255.

  When the constant and variable conform to these restrictions, the variable is initialized with the character that has the ASCII code specified by the constant. (This lets you initialize a character object to any 8-bit ASCII code.)

- If the constant is a Hollerith or character constant, and the variable is a numeric variable or numeric array element, the number of characters that can be assigned depends on the data type of the data item.

  If the Hollerith or character constant contains fewer characters than the capacity of the variable or array element, the constant is extended on the right with blank characters. If the constant contains more characters than can be stored, the constant is truncated on the right.

Examples

The following example shows the three ways that DATA statements can initialize array element values:

    DIMENSION A(10,10)

    DATA A/100*1.0/    ! initialization by name

    DATA A(1,1), A(10,1), A(3,3) /2*2.5, 2.0/ ! initialization by element

    DATA ((A(I,J), I=1,5,2), J=1,5) /15*1.0/  ! initialization by implied-do list

The following example shows DATA statements containing structure components:

    TYPE EMPLOYEE
      INTEGER ID
      CHARACTER(LEN=40) NAME
    END TYPE EMPLOYEE
    TYPE(EMPLOYEE) MAN_NAME, CON_NAME
    DATA MAN_NAME / EMPLOYEE(417, 'Henry Adams') /
    DATA CON_NAME%ID, CON_NAME%NAME /891, "David James"/

In the following example, the first DATA statement assigns zero to all 10 elements of array A, and four asterisks followed by two blanks to the character variable STARS:

    INTEGER A(10), B(10)
    CHARACTER BELL, TAB, LF, FF, STARS*6
    DATA A,STARS /10*0,'****'/
    DATA BELL,TAB,LF,FF /7,9,10,12/
    DATA (B(I), I=1,10,2) /5*1/

In this case, the second DATA statement assigns ASCII control character codes to the character variables BELL, TAB, LF, and FF. The last DATA statement uses an implied-do list to assign the value 1 to the odd-numbered elements in the array B.

As a Fortran 95 feature, a pointer can be initialized as disassociated by using a DATA statement. For example:

    INTEGER, POINTER :: P
    DATA P/NULL( )/
    END

For More Information:

- On implied-do lists, see Section 10.2.2.
- On initialization and specification expressions, see Section 4.1.7.
- On type declaration statements, see Section 5.1.

Previous Page Next Page Table of Contents