Scanning a Format: I

All examples presented in previous pages are simple. The number of variables and the number of edit descriptors are equal. In real applications, this is rarely the case. This page discusses a simple case; more complicated cases will be discussed later after more edit descriptors are covered.

Note that all edit descriptors discussed so far (i.e., I, F, E, L and A) are for printing the value of a variable or an expression. This makes our discussion easier. Recall that a WRITE always starts with a line. Here, a "line" should be interpreted as a buffer line discussed in the page of printer control.

When a formatted WRITE is executed, Fortran starts scanning the format from the left end (i.e., the edit descriptor next to the left parenthesis). For each variable or the result of an expression of the WRITE statement, Fortran uses the next available edit descriptor. Then, please remember the following:

The type of the value and the edit descriptor used to print it must match. Integers use edit descriptor I, reals use F or E, logicals use L, and character strings use A. Without observing this rule, your program may compile; but, it will cause execution error when that format is used.

In the example below, we have four variables to print. The first one is Age whose type is INTEGER. Fortran starts scanning the format from the left parenthesis and finds the first edit descriptor I4. So, I4 is for Age. Then, the next variable is Salary whose type is REAL. To print it, Fortran finds the next edit descriptor F10.2. Thus, F10.2 is for Salary. The next variable is State whose type is CHARACTER. So, Fortran searches for the next edit descriptor A. Thus, A (actually A8) is for CHARACTER variable State. The next variable is InState whose type is LOGICAL and the next editor descriptor is L2. Thus, InState is printed using L2. Now, all four variables are processed successfully into a buffer line and printed with a printer.

CHARACTER(LEN=8) :: State   = "Michigan"
INTEGER          :: Age     = 30
REAL             :: Salary  = 32165.99
LOGICAL          :: InState = .TRUE.

WRITE(*,"(I4, F10.2, A, L2)")  Age, Salary, State, InState

How about the following example? INTEGER variable Age is printed using I4, CHARACTER variable State is printed using F10.2. This is a mistake, because CHARACTER variables must be printed with the A edit descriptor and because the F descriptor can only print REAL values. Consequently, if you run this program, you will get a run time error when this WRITE is executed.

CHARACTER(LEN=8) :: State   = "Michigan"
INTEGER          :: Age     = 30
REAL             :: Salary  = 32165.99
LOGICAL          :: InState = .TRUE.

WRITE(*,"(I4, F10.2, A, L2)")  Age, State, Salary, InState

What If the Numbers of Variables and Edit Descriptors Are not Equal?

Sooner or later you will ask this important question. We have two cases to consider:

There are two more rules, one for those edit descriptors that do not require input or output values (i.e., nX, Tc, TLc, TRc and /), while the other for grouping. Click here to see the second rule, and here for the third.

Why Are These Complicated Rules Necessary?

If you have only a few variables to print, these rules seems redundant because we can always design a format that completely covers all variables. However, when arrays come into the picture, the situation becomes more complicated since in general we do not know the number of array elements to be printed. For example, in the first run of your program, the array may contain 10 elements; but the array could contain 100 elements in the next run. Therefore, the rules discussed above offer some flexibility. Here are two simple examples: