# DATA

Statement: Assigns initial values to variables before program execution.

DATA var-list /clist/ [[,] var-list /clist/]...

-or-

DATA (var = const (, var = const)...)

var | Is a variable or implied-DO list, separated by commas. var cannot be a coarray, a dummy argument, accessed by use or host association, a function name, a function result name, an automatic variable, or an allocatable variable. Subscript expressions, section expressions, and substring expressions must be constant expressions. An implied-DO list in a DATA statement takes the following form: ( do-list, [integer-type-spec ::] do-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.
integer-type-spec | Is INTEGER [kind-selector].
kind-selector | Is ( [KIND=] n ).
n | Is a constant expression whose value is 1, 2, 4, or 8.
do-var | Is the name of a scalar integer variable (the implied-DO variable). It cannot be a coarray.
expr | Implied-DO limits must be scalar constant expressions. They may contain implied-DO variables from outer nested implied-DO lists. For more details, see Iteration Loop Control.
var-list | Is a list of vars separated by commas.
const | Is a scalar constant expression (or name of a constant), constant structure or array constructor, or, for pointer objects, NULL (). If the constant is a binary, octal, or hexadecimal literal, the corresponding var must be of type INTEGER. A constant can be specified in the form r*constant, where r is a repeat specification. r is a nonnegative scalar integer constant (with no kind parameter). If r 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.
clist | Is a list of consts separated by commas.

## Description

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 which may change 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. If the associated constant list does not contain enough values to fill the array, a warning is issued and the remaining array elements become undefined.

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. An error results if any nonzero digits are truncated.

- 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.

## Example

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

      DIMENSION A(10,10), B(4)
      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
      DATA ((B(i), I = 1, 4) = [11.0, 12.0, 13.0, 14.0])

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.

The following shows another example:

        INTEGER n, order, alpha, list(100)
        REAL coef(4), eps(2),
        pi(5), x(5,5)
        CHARACTER*12 help
        COMPLEX*8 cstuff
        DATA  n /0/, order /3/
        DATA  alpha /'A'/
        DATA  coef /1.0, 2*3.0, 1.0/, eps(1) /.00001/
        DATA  cstuff /(-1.0, -1.0)/
    !    The following example initializes diagonal and below in
    !    a 5x5 matrix:
        DATA  ((x(j,i), i=1,j), j=1,5) / 15*1.0 /
        DATA  pi / 5*3.14159 /
        DATA  list / 100*0 /
        DATA  help(1:4), help(5:8), help(9:12) /3*'HELP'/

Consider the following:

     CHARACTER (LEN = 10) NAME
     INTEGER, DIMENSION (0:9) :: MILES
     REAL, DIMENSION (100, 100) :: SKEW
     TYPE (MEMBER) MYNAME, YOURS
     DATA NAME / 'JOHN DOE' /, miles / 10*0 /
     DATA ((SKEW (k, j), j = 1, k), k = 1, 100) / 5050*0.0 /
     DATA ((SKEW (k, j), j = k + 1, 100), k = 1, 99) / 4950*1.0 /
     DATA MYNAME / MEMBER (21, 'JOHN SMITH') /
     DATA YOURS % age, YOURS % name / 35, 'FRED BROWN' /

In this example, the character variable NAME is initialized with the value JOHN DOE with two trailing blanks to fill out the declared length of the variable. The ten elements of MILES are initialized to zero. The two-dimensional array SKEW is initialized so that its lower triangle is zero and its upper triangle is one. The structures MYNAME and YOURS are declared using the derived type MEMBER. The derived-type variable MYNAME is initialized by a structure constructor. The derived-type variable YOURS is initialized by supplying a separate value for each component.

The first DATA statement in the previous example could also be written as:

     DATA name / 'JOHN DOE' /
     DATA miles / 10*0 /

A pointer can be initialized as disassociated by using a DATA statement. For example:

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

The implied-DO limits can be any constant expressions in a DATA statement. For example:

     DATA (A(I),I=LBOUND(A),UBOUND(A)) /10*4.0/

Parent topic: C to D

## See Also

CHARACTER
INTEGER
REAL
COMPLEX
COMMON
Data Types, Constants, and Variables
I/O Lists
Derived Data Types
Allocating Common Blocks