Fortran Formats

We have discussed the READ and WRITE statements. These are the so-called list-directed input/output statements. They are also referred to as free-format input/output statements. List-directed input/output statements are easy to use; however, we have no control over the appearance of the input and output. To overcome this problem, we should use formats.

Fortran formats are used to control the appearance of the input and output. It has the following simple form:

( ..... format edit descriptors ..... )
That is, a Fortran format is a pair of parenthesis that contains format edit descriptors separated by commas.

There are three possible ways to prepare a Fortran format. Fortran has a FORMAT statement; but, we will not use it because the two methods discussed below offer higher level of flexibility.

Format Edit Descriptors

The tedious part of using Fortran format is to master many format edit descriptors. Each edit descriptor tells the system how to handle certain type of values or activity. Each value requires some positions. For example, an integer of four digits requires at least four positions to print. Therefore, the number of positions to be used is the most important information in an edit descriptor.

We shall use the following convention of symbols:

Although we may print a number using as many positions as you want, this is only for input/output. This number of positions is not the precision (i.e., the number of significant digits) of that number. To be more precisely, computers normally can store real numbers up to seven significant digits. This is the precision of real numbers. However, we can print a real number using 50 positions in which 25 positions are for the fraction part. This is only a way of describing the appearance and does not change the precision of real numbers.

The following are the editor descriptors to be discussed. Details will be given on subsequent pages.

Purpose Edit Descriptors
Reading/writing INTEGERs Iw Iw.m
Reading/writing REALs Decimal form Fw.d
Exponential form Ew.d Ew.dEe
Scientific form ESw.d ESw.dEe
Engineering form ENw.d ENw.dEe
Reading/writing LOGICALs Lw
Reading/writing CHARACTERs A Aw
Positioning Horizontal nX
Tabbing Tc TLc and TRc
Vertical /
Others Grouping r(....)
Format Scanning Control :
Sign Control S, SP and SS
Blank Control BN and BZ

Most edit descriptors can be repeated and several edit descriptors can be grouped into a group. For most of the cases, edit descriptors are separated by commas. The following is an example:

CHARACTER(LEN=30) :: Format

Format = "(5X, I5.2, F10.3, A, ES14.7)"

READ(*,Format)  ... variables ...

WRITE(*,Format) ... variables and expressions ...
In the above example, format Format has five edit descriptors 5X, I5.2, F10.3, A and ES14.7. Adjacent edit descriptors are separated by a comma.