Chapter 4
Essentials of Essentials of Fortran-90/95/2003
More on Formats

Math 60 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics and Computer Science

[Return to Math 60 Homepage | Return to Brief Contents Page]
[Return to Full Contents Page]

Contents


4.1 Additional Format Edit (Field) Descriptors

The following list contains other commonly-used edit descriptors. As with the list in section 3.5, after the descriptor's letter-number combination follows the corresponding data type and an explanation of the number. The text will usually refer to FORMAT statements, but whatever is said also applies to the equivalent formatting information that can be included within a READ/WRITE statement (as described in section 3.9).
An Alphanumeric:
used for conversion of character data for either output or input:
n corresponds to the number of characters associated with the variable, i.e., the field width.
In Fortran-90/95/2003 (and FORTRAN-77), characters are normally stored in variables declared in character type declarations, but in FORTRAN-IV, standard practice was to store characters in integer variables. Further information regarding character variables is given in Chapter 13.
Dw.d Double-Precision:
used for real numbers declared as double precision: The same rules hold as for the E field descriptor, except that a D is (usually) printed in the output instead of an E.
Tn Tabulation:
on OUTPUT, it is used to tabulate to output space n (if the carriage control column is operative, this would correspond to visible column n-1). For example, if T30 were to occur in a FORMAT statement immediately before I5, the I5 field would consist of columns 30 through 34 (or columns 29 through 33 if the carriage control column is operative).
on INPUT, the input space and the visible space are the same, so that T30 before I5 would mean that the I5 field would consist of columns 30 through 34. On INPUT, the T descriptor can be used to re-read part or all of the input line.
TLn
TRn
(Relative) Tabulation (Left or Right):
Whereas the T field descriptor specifies absolute tabulation to a specific column, the TL (tabulate left) and TR (tabulate right) specify relative tabulation, that is, moving n spaces right or left from the end of the previous field. Thus, TR works exactly the same as the X descriptor. On OUTPUT, TL will not cause any overprinting, but on INPUT, it can be used to re-read part or all of the INPUT line.
Gw.d General:
used in lieu of E or F. In some implementations, it may also be used in lieu of I, D and A as well. It adjusts the format to match the type of the input data or the output variable.
Ln Logical:
on OUTPUT, a T or an F is printed right justified in a field of n blanks.
on INPUT, n characters are read, and only the first non-blank character is examined. If it is not a T or F (upper or lower case), an error occurs. The T or F may be preceded by a period.
S, SS, SP (Plus) Sign:
on OUTPUT, SP signals that the numeric descriptors which follow it should print a plus sign before positive numbers. SS signals that the numeric descriptors which follow it should print a BLANK in lieu of a plus sign. Each descriptor can negate the effect of the other descriptor if more than one descriptor is used successively in one FORMAT statement. The effect of the descriptor last only until the end of the FORMAT statement in which it occurs. S returns to the default style of the Fortran version being used. (On most systems, S acts like SS.)
nP scaling factor for F, E, G, or D input or output:
It immediately precedes the key letter of the numeric input descriptor that it scales. E.g., 2PF10.4
on INPUT, the number read is multiplied by 10**(-n) before storage, except if it contains an explicit exponent.
on OUTPUT, the stored number does not change, but its printed value may not correspond to the internal value. With F editing, the output is the internal value multiplied by 10**n. With D or E editing, the output value is the same as the internal value, but the representation has been shifted so that the exponent is reduced by n, and the mantissa is multiplied by 10**n. With G editing, the effect is based on wither E or F editing is ultimately chosen, in which cases the normal P effect takes place.
The field descriptors mentioned here and earlier in section 3.5 are the more common ones used. One should check the manual of the Fortran version being used for other possible field descriptors.

4.2 Multiple Repetitions

One can use parentheses to induce repetitions of field descriptors within FORMAT statements, even if the field descriptors themselves have repetition factors. For example,
	FORMAT(3X,3(I10,2F10.5))
is equivalent to
	FORMAT(3X,I10,2F10.5,I10,2F10.5,I10,2F10.5)
which itself is equivalent to a FORMAT statement with double the number of F10.5 descriptors in it (since each F10.5 descriptor itself has a multiplication factor of two before it)!

4.3 Input Format

Certain care must be taken when working with FORMAT statements associated with READ statements. The following rules should be remembered:

1) There is NO CARRIAGE CONTROL in input;

2) Blanks are usually interpreted as ZEROES (but some systems switch the default on this);

3) Any decimal point present in the input, supersedes what is suggested by any E, F, D, or G descriptor, otherwise if no decimal point exists among the input digits, one is "forced" by determining how many digits are to be considered to the right of the (non-existent) decimal point according to the field descriptor;

4) There is no distinction between E, F and G descriptors for input.

Whether (2) holds is system dependent. On some systems, blanks are normally treated as zeroes, but on others, they are ignored. To avoid the uncertainty this may cause, and to allow the programmer the determine how blanks should be interpreted for a particular program, Fortran-90/95/2003 (and FORTRAN-77) has included two field descriptors for INPUT use to avoid any confusion in this matter.

BZ,BN Blanks are Zero / Blanks are Null:
On INPUT, BZ signals that the numeric descriptors which follow it should treat blanks are zeroes. BN signals that the numeric descriptors which follow it should ignore blanks and concatenate all digits together to form the input numeric value. Each descriptor can negate the effect of the other descriptor if more than one descriptor is used successively in one FORMAT statement. The effect of the descriptor last only until the end of the FORMAT statement in which it occurs.
NOTE: If a numeric field which follows a BN descriptor contains all blanks, the numeric value is assumed to be zero.

4.4 Input Format Example

		READ(5,101) A,I
		101 FORMAT(BZ,F10.5,I5)
Suppose the input line consisted of the following:
column number -->
  1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16
                  1   2                            3
The variable A would receive the value of 1.2 and I the value of 300.

EXPLANATION: Descriptor BZ causes the rest of the FORMAT to treat blanks as zeroes. Field descriptor F10.5 allocates columns 1 through 10 for variable A and says that the last 5 columns will be considered to the right of the decimal point (unless a decimal point present forces a different placement). Thus, numbers in columns 6 through 10 are to the right of the decimal point. Therefore, the decimal point goes between 1 and 2. The next field descriptor I5 allocates the next five columns, columns 11 through 15 for the next variable, I. Since columns 14 and 15 are blank, the blanks are interpreted as zeroes, and I receives the value of 300.

Suppose the following input line were used instead:

column number -->
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16
                              1   .   2   3       3       2   3

Then A would receive the value of 1.2 and I the value 30302.
EXPLANATION: As before, columns 1 through 10 are reserved for variable A. Since there is now a decimal point included in the input, the 5 in the field descriptor is ignored. As before, the next five columns are reserved for the value of I and blanks are interpreted as zeroes. Thus we get 30302.

The 3 in column 16 is ignored.

4.5 Formats: Underuse and the Colon Field Separator

Our assumption thus far has been that the number of items listed in the READ or WRITE statement matches the number of field descriptors found in the corresponding FORMAT statement. In general, this does not have to be the case. It is permitted to have fewer or more field descriptors in a FORMAT statement than variables in the corresponding input or output statement. However, in either case special rules apply.

If there are fewer variables than format field descriptors, the READ or WRITE uses as many as are needed to print out all the variables, and then ignores the rest of the the descriptors in the FORMAT statement. Thus, the following is legal Fortran code:

		WRITE(6,101) I,J,K
		WRITE(6,101) L,M,N1,N2,N3
		101 FORMAT(1X,10I5)
Note that there are more than enough numeric field descriptors (10 integer fields) in FORMAT 101 for either WRITE statement.

If the FORMAT statement has a literal character string in it immediately before the first field descriptor which is not being used, the literal character string will still be printed out! In other words,

		I = 2
		WRITE(6,101) I
		101 FORMAT(1X,'I=',I2,' and J=',I2)
will print out:
                I= 2 and J=
In general, Fortran will continue to try to use field descriptors until nothing is left in the READ/WRITE statement. However, if it is desirable to omit printing the second literal character string, one can precede the string with a colon, :, instead of a comma. In other words,
		I = 2
		WRITE(6,101) I
		101 FORMAT(1X,'I=',I2:' and J=',I2)
will print out:
                I= 2

4.6 Formats: Overuse

If there are more variables than FORMAT field descriptors, then the FORMAT statement is re-used until all the variables are either read into or written out. As usual, each re-use of the associated FORMAT statement starts a new input/output line.

Let us look at the following example.

		WRITE(6,102) A1,A2,A3,A4,B1,B2,B3,B4
		102 FORMAT(1X,2F20.5)
Here, FORMAT 102 only provides for two numeric fields. In contrast, the WRITE statement contains eight variables. Therefore, FORMAT 102 gets re-used 4 times, each time starting a new line (because of the blank carriage control character induced by the initial 1X). However, if FORMAT 102 were changed to allow three numbers per line, the output would consist of three lines, with three variables per line for the first two lines, and two variables on the last line. The order of the variables would be the same, i.e. A1, then A2, then A3, then A4, then B1, then B2, then B3, and finally B4.

There is one minor variation of the re-use rule when the FORMAT statement itself contains parentheses (used for multiple repetitions, e.g. 3(2I5,3F10.5) ). In these cases, the entire FORMAT statement is not re-used. Only the last parenthesized multiple repetition up to the end of the FORMAT is re-used. For example, if the following format has to be re-used:

               103 FORMAT(1X, I5, (1X,2F10.5))
then the first line would have three numbers on it (corresponding to I5, F10.5, and F10.5) and the remaining (repetition) lines would only have two numbers on them (corresponding to what is in the (last and only) group repetition (even though it has no preceding repetition factor).

4.7 `Format Free' Input and Output

As mentioned in section 3.3, Fortran-90/95/2003 (and FORTRAN-77) allows the use of an asterisk (*) instead of a FORMAT statement number in a READ or WRITE statement. This causes the compiler to match the appropriate field descriptor (of appropriate size) to the variable (or constant) in the variable list of the READ/WRITE statement.

For example,

	WRITE(6,*) I,A,B,J
or
	READ(5,*) A,B,J
or
	WRITE(6,*) ' I =',I

4.8 Other Input and Output Commands

Other input/output commands exist in Fortran-90/95/2003 (and FORTRAN-77). Often they omit the use of a device number and/or the FORMAT number, since frequently they specify one particular device.

For example,

	PRINT n, variable list
directly prints on the standard output device (i.e., the computer screen) according to format statement n.

(Older versions of Fortran also included a TYPE statement for printing on the terminal screen, but Fortran-90/95/2003 uses the keyword TYPE in a different way. Similarly, older versions of Fortran also included a PUNCH statement for punching data directly onto IBM cards.)

In addition, file positioning statements are also available, which are especially useful when using magnetic tapes, but can be applied to any sequential access external file. These statements are:

BACKSPACE n position file with unit number n before the previous record.
ENDFILE n write end of file record on file with unit number n.
REWIND n position file with unit number n at the beginning.

This page is maintained by Dennis C. Smolarski, S.J. dsmolarski@math.scu.edu
© Copyright 1998, 1999, 2000, 2005 Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 5 June 2005.