17. Format statements

So far we have mostly used free format input/output. This uses a set of default rules for how to input and output values of different types (integers, reals, characters, etc.). Often the programmer wants to specify some particular input or output format, e.g., how many decimals places in real numbers. For this purpose Fortran 77 has the format statement. The same format statements are used for both input and output.

Syntax

      write(*, label) list-of-variables
 label format format-code

A simple example demonstrates how this works. Say you have an integer variable you want to print in a field 4 characters wide and a real number you want to print in fixed point notation with 3 decimal places.

      write(*, 900) i, x
  900 format (I4,F8.3)

The format label 900 is chosen somewhat arbitrarily, but it is common practice to number format statements with higher numbers than the control flow labels. After the keyword format follows the format codes enclosed in parenthesis. The code I4 stands for an integer with width four, while F8.3 means that the number should be printed using fixed point notation with field width 8 and 3 decimal places.

The format statement may be located anywhere within the program unit. There are two programming styles: Either the format statement follows directly after the read/write statement, or all the format statements are grouped together at the end of the (sub-)program.

Common format codes

The most common format code letters are:

   A - text string
   D - double precision numbers, exponent notation
   E - real numbers, exponent notation
   F - real numbers, fixed point format
   I - integer
   X - horizontal skip (space)
   / - vertical skip (newline)

The format code F (and similarly D, E) has the general form Fw.d where w is an integer constant denoting the field width and d is an integer constant denoting the number of significant digits.

For integers only the field width is specified, so the syntax is Iw. Similarly, character strings can be specified as Aw but the field width is often dropped.

If a number or string does not fill up the entire field width, spaces will be added. Usually the text will be adjusted to the right, but the exact rules vary among the different format codes.

For horizontal spacing, the nX code is often used. This means n horizontal spaces. If n is omitted, n=1 is assumed. For vertical spacing (newlines), use the code /. Each slash corresponds to one newline. Note that each read or write statement by default ends with a newline (here Fortran differs from C).

Some examples

This piece of Fortran code

      x = 0.025
      write(*,100) 'x=', x
  100 format (A,F)
      write(*,110) 'x=', x
  110 format (A,F5.3)
      write(*,120) 'x=', x
  120 format (A,E)
      write(*,130) 'x=', x
  130 format (A,E8.1)

produces the following output when we run it:

x=      0.0250000
x=0.025
x=  0.2500000E-01
x= 0.3E-01

Note how blanks are automatically padded on the left and that the default field width for real numbers is usually 14. We see that Fortran 77 follows the rounding rule that digits 0-4 are rounded downwards while 5-9 are rounded upwards.

In this example each write statement used a different format statement. But it is perfectly fine to use the same format statement for many different write statements. In fact, this is one of the main advantages of using format statements. This feature is handy when you print tables for instance, and want each row to have the same format.

Format strings in read/write statements

Instead of specifying the format code in a separate format statement, one can give the format code in the read/write statement directly. For example, the statement

 
      write (*,'(A, F8.3)') 'The answer is x = ', x

is equivalent to

 
      write (*,990) 'The answer is x = ', x
  990 format (A, F8.3)

Sometimes text strings are given in the format statements, e.g. the following version is also equivalent:

 
      write (*,999) x
  999 format ('The answer is x = ', F8.3)

Implicit loops and repeat counts

Now let us do a more complicated example. Say you have a two-dimensional array of integers and want to print the upper left 5 by 10 submatrix with 10 values each on 5 rows. Here is how:

      do 10 i = 1, 5
         write(*,1000) (a(i,j), j=1,10)
   10 continue
 1000 format (I6)

We have an explicit do loop over the rows and an implicit loop over the column index j.

Often a format statement involves repetition, for example

  950 format (2X, I3, 2X, I3, 2X, I3, 2X, I3)

There is a shorthand notation for this:

  950 format (4(2X, I3))

It is also possible to allow repetition without explicitly stating how many times the format should be repeated. Suppose you have a vector where you want to print the first 50 elements, with ten elements on each line. Here is one way:

      write(*,1010) (x(i), i=1,50)
 1010 format (10I6)

The format statement says ten numbers should be printed. But in the write statement we try to print 50 numbers. So after the ten first numbers have been printed, the same format statement is automatically used for the next ten numbers and so on.

Implicit do-loops can be multi-dimensional and can be used to make an read or write statement difficult to understand. You should avoid using implicit loops which are much more complicated than the ones shown here.

Copyright 1995-7 by Stanford University. All rights reserved.