Formatted Input and Output

We have some choices when we read input or write output. Often we use the 'default format', as in:

         READ *, A, B
         PRINT *, A, B

If we want more control over the process, we can use the FORMAT statement, which contains a 'format string', or we can include a format string in the READ or PRINT statements.

Examples of READ

         READ *, NUM                    ! use the default format
         READ 200, NUM1, NUM2           ! use the FORMAT statement labeled 200
         READ '(I3,2X,I7'), NUM1, NUM2  ! use the quoted format string

For the second READ statement to make sense, we need to have a statement of the form:

    200  FORMAT(I3,2X,I7)

Examples of PRINT

         PRINT *, NUM                   ! use the default format
         PRINT 150, NUM, NUM2           ! use the FORMAT statement labeled 200
         PRINT '(I7,4X,I6)', NUM1, NUM2 ! use the quoted format string

For the second PRINT statement to make sense, we need to have a statement of the form:

    150 FORMAT(I7,4X,I6)


Default Format for Input

If we want to read using the default format, the input record should contain a sequence of values separated by (one or more) blanks. The values should match the types of data of the variables listed to receive those values.

For an INTEGER variable, the input value should a sequence of digits perhaps preceded by a plus or minus sign.

For a REAL value, the input value should be a sequence of digits perhaps including one decimal point.

For a CHARACTER variable, the input value should be enclosed in quotation marks. (It is legal to have an empty string '' as a value.) The quotation marks are not part of the value but serve as delimiters.

If there are not enough values in the input record to match the variables listed, reading continues with the next record.

It is possible to have repeated values in input. This involves using a repetition factor; we could have n*value in the input. An example might be:

         14  567  3*72

where this line is considered now to contain 5 input integer values.


Default Format for Output

When we write output using the default format, the appearance of numeric values is not entirely predictable. Numeric values are written separated by blanks. INTEGER values are printed using the number of digits needed for the largest available integer value, without leading zeroes, preceded by a minus sign if appropriate. REAL values are printed using either the E or F format depending on the size of the number.

CHARACTER variables and literals are printed in their exact length without preceding or following blanks.

A LOGICAL value is written as 'T' for the value .TRUE. or 'F' for the value .FALSE..

Lines are printed are (by default) single-spaced.


Format Strings

A format string is a list of descriptors and literals, usually separated by commas, enclosed in parentheses. It may be coded into a READ, WRITE or PRINT statement, as in

         PRINT '(A8,1X,I4)', NAME, NUM

or it may be the body of a FORMAT statement, as in

         200   FORMAT(A8,1X,1X,I4)

in which case we refer to it by the label on the FORMAT statement, as in PRINT 200, NAME, NUM

A format string can contain format substrings marked with extra parentheses, and such substrings can have repetition factors. An example of this might be

         (A7,4(2X,I3),A5)

which is equivalent to

         (A7,2X,I3,2X,I3,2X,I3,2X,I3,A5)

Substrings can be useful when we want to read or write a number of value of the same type, perhaps using an array.


Output Descriptors

Note: This list is not exhaustive. In particular, it does not cover DOUBLE PRECISION or COMPLEX values, and it does not cover scale factors.

Note about the following: The I, F, E, A and L descriptors can all be prefixed with repetition factors. We might have 3I4, 4F6.2, or 2A10.


Input Descriptors

Note about the following: The I, F, E, A and L descriptors can all be prefixed with repetition factors. We might have 3I4, 4F6.2, or 2A10.

Note: This list is not exhaustive. In particular, it does not cover DOUBLE PRECISION or COMPLEX values, and it does not cover scale factors.


Further Comments

One of the reasons to use a FORMAT statement is that we may want to print a number of lines using the same format. If we hard-code the format string into our PRINT statements, then (a) we have to do more work, typing the same string repeatedly, and (b) if we decide later that we want to change it slightly, we will need to make changes in a number of places instead of one. Using FORMAT statements is therefore a convenience.

FORMAT statements are considered data and are not executable code. They may occur anywhere in a program; the compiler will recognize their nature and not attempt to execute them. It makes sense, however, to list them not too far from where they are used.