Using the Format Statement


Assignment :

Finish Homework 9

New Fortran:

Format edit descriptors, internal files

I gave you a brief introduction to the FORMAT statement, when we first discussed arrays. Now it is time look at its capabilities more thoroughly. The contents of a FORMAT statement are a simple command language imbedded within Fortran. In fact, problems with the FORMAT are often not detected until execution of the program because the format instructions between the parentheses of the FORMAT statement are not actually interpreted until execution time.

Always realize that a FORMAT does not really exist in isolation. It is tightly coupled to a WRITE, PRINT, or READ. I will restrict my examples here to use with WRITE (or PRINT). My view is that (with very rare exceptions) READ should always be used with the default format

      read *, a,b,c
      read (11,*) i,j,k
or in a mode where the entire line (record) is read into a single CHARACTER variable

      read (11,1000) line
      read 1000, line
 1000 format (a)
where "a" is the edit descriptor appropriate for CHARACTER variables and line is a CHARACTER variable long enough to hold a full record (at least CHARACTER*80). I will give examples in a later section, showing how to process "line" after this READ to obtain any needed input information, including integers and real numbers.

For WRITEs there is a matching between variables listed after the right parenthesis in the WRITE, and the edit descriptors for variables ( "i", "f", "e", etc.).. For

      write(*,1001) icount, x, y
 1001 format (i5,f5.2,e12.3)
The "i5" is the first variable descriptor in the FORMAT and tells you how to output the first variable listed after "write(*,1001)" , here "icount". The "f5.2" is the second variable descriptor in the FORMAT and tells you how to output the second variable after the WRITE ("x"). The "e12.3" is the third variable descriptor and tells you how to output the third variable being written ("y"). Once this skeleton is in place, quoted labels, spaces, and other descriptors that don't refer directly to the variables in the WRITE can be added to provide clarity to the output line.

 1001 format ( ' At icount = ', i5, '  x = ', f5.2,5x, 'y = ',
     &    1p,e12.3)
The "5x" inserts 5 blank spaces between the end of the printed value of "x", and the beginning of the printed string "y =". The "1p" provides a better form of the exponential output, giving a non-zero digit to the left of the decimal point.

There are four steps that you should follow in laying out a Format for output.

  1. Decide on the range of magnitude of the numbers being output. For integers this simply determines the number of positions to reserve in the line. For real numbers this will determine whether it is appropriate to output in simple decimal format or exponential format (scientific notation). To completely determine the line space required for a real number, you will have to determine the decimal precision that you want on your output. You do not want more decimal precision printed than is contained in the internal floating point number. In fact, unless you are checking numerical results, you generally want substantially less. Think about what you need to compare with real world results.
  2. Decide what labels are necessary for clear interpretation of the data.
  3. Decide if the combination of labels and data is best represented on one line, or if more than a single line is appropriate.
  4. Lay out a sample line (or lines) with sample numbers, and clear markers for column positions. This is best done with a text editor or a fixed pitch font (e.g. Courier) on a word processor.
         1         2         3         4         5         6      
123456789012345678901234567890123456789012345678901234567890
      Station  1:  Average Temperature was  70.3 F
                   Minimum Temperature was  50.1 F
                   Maximum Temperature was 100.3 F
                   Noon Temperature was  90.2 F

After this preparation, select and program the appropriate FORMAT edit descriptors to place each element of your formatted output into the correct position. Be sure to check your programming by running your program and comparing output directly against your original sample.

I won't cover the full list of FORMAT edit descriptors, but will give you a basic set to cover just about any need. Consult a Fortran manual for other descriptors. The first set of edit descriptors mark places for input or output of variables listed in the I/O statement (READ, WRITE, PRINT).

I or i

Marks the place for an integer variable. It is followed by an integer indicating the number of columns to be reserved in the line for the integer. For example "i10", reserves 10 columns for output of the integer. If the integer contains less that 10 digits in this example, it is right justified in the reserved 10 columns and unused columns simply appear as spaces to the left of the integer.
F or f

Marks the place for the decimal form of a real number. As with "i", it is followed by an integer reserving a number of columns for the number. This integer is followed immediately by a period ("."), acting as a separator rather than true decimal place. The period is immediately followed by another integer giving the number of digits to be printed (or read) to the right of the number's decimal. For example if the computer contains the number 11.23456, then "f10.3" in an output format results in a printed number " 11.234" (four spaces to the left of the number).
E or e

Marks the place for the exponential form of a real number. As with "f", it is followed by an integer reserving a number of columns for the number, a period, acting as a separator, and another integer giving the number of digits to be printed (or read) to the right of the number's decimal. For example if the computer contains the number 11.23456, then "e10.3" in an output format results in a printed number " 0.123e+02" (one space to the left of the number).
A or a

Marks the place for a character variable. It is followed by an integer indicating the number of columns to be reserved in the line for the character string. For example "a10", reserves 10 columns for output of the characters. If the character variable's length is less than 10 in this example, its contents are right justified within the 10 columns. If the character variable's length is greater than 10 for an "a10", the first 10 characters of the variable are used and the rest ignored.
L or l

Marks the place for an logical variable (infrequently used). It is followed by an integer indicating the number of columns to be reserved in the line for the logical variable. For example "L10", reserves 10 columns for output of the logical value. Depending on the value of the logical variable either a "T" or a "F" will be placed in the last reserved column (the rest are blank).

It is worth noting that Fortran 90 provides standard edit descriptors for binary (B), octal (O), and hexadecimal (Z) numbers. Think of them as "I" for a different base arithmetic. However, due to low frequency of use, place these edit descriptors low on your list of things to remember..

Any of the above descriptors can be preceded by a repeat count, integer indicating how many sequential variables should be processed with the following edit descriptor. The variables "w", "x", "y", and "z" could be output in the same format with the following

      write(11,2000) w, x, y, z
 2000 format(4e15.3)
Note that I'm careful to reserve enough columns in the "e" descriptor to provide spacing between the numbers on the output. This repeat count can be combined with parentheses to obtain more complicated results.

     write(11,2001) i, x, j, y, k, z
 2001 format ( 3(i5,e15.3))
produces the proper pairs alternating between and integer and exponential format.

The results of the "e" and "f" edit descriptors can be modified with the "P" (or "p") descriptor. It's primary use is to shift the decimal in "e" formatted numbers. For the internal number 11.23456, the sequence "1p,e10.3" produces " 1.123e+01". The shift in the decimal point is accompanied by an appropriate shift the value of the exponent. The sequence "2p,e10.3" produces the result "11.234e+00". For "f" format the "p" edit descriptor shifts the decimal point, but no exponent exists to balance this action so for the same internal number, "1p,f10.3" gives and output of " 112.345", the wrong value. Once a "p" descriptor appears in a FORMAT, it remains in effect until overridden by another "p". If "f" descriptors follow "e" descriptors, you can cancel use of "1p" with "0p". For example : ( 1p,3e10.3, 0p,3f10.3), produces proper values for all 6 real numbers. In Fortran 90 the need for "1p" is eliminated with the introduction of the "es" edit descriptor. "es10.3" produces results equivalent to "1p,e10.3".

Labels are provided in FORMATs through inclusion of quoted strings, bounded by single quotes:

 2002 format ( ' Temperature = ', f5.1)
If a single quote is needed within a label, it must be represented as two consecutive single quotes in the FORMAT

 2003 format (' I''m sure Temperature = ', f5.1)
If you need spacing in a line not accounted for in extra length of a variable edit descriptor, use "x" or "X". It can by preceded by a repeat count.

 2004 format (5x,'Temperature',10x,'Pressure')
precedes the label "Temperature" with 5 spaces in the line and follows it with 10 spaces before printing "Pressure". The function of "X", plus a little more power can be obtained with "Tab" edit descriptors (T, TL, and TR), but these are not on my need to remember list. I have personally survived quite well with extremely low use of the "Tab" features.

If you wish to span more than one record (line) in a FORMAT, simply include "/" to tell the computer to go to the beginning of the next record (line). Several of these can be used sequentially to skip multiple lines, or in Fortran 90, the "/" may be preceded by a repeat count (e.g. "5/").

You must master one final aspect of FORMATs before their full power is available to you. When reading or writing arrays, you generally run out of variable edit descriptors in the FORMAT statement before running out of variables to read or write. This is no problem if you understand and correctly use the wrap around feature of a FORMAT.

After all variable descriptors are used in a FORMAT, a new line (record) is started. To control the format of remaining variables listed in the WRITE (or READ), the FORMAT wraps around to the last left parenthesis (unless that parenthesis is preceded by a repeat descriptor) in the FORMAT, and all contents of the FORMAT beyond that parenthesis are reused until all variables are printed, or another FORMAT wrap around is needed. If the last left parenthesis is preceded by a repeat count, Fortran looks to see if this repeated segment is nested within another pair of parentheses (other than those required by the FORMAT statement). If such bounding parentheses exist, the wrap-around goes to the left parenthesis of this pair.

Examples of this wrap around and other formatting features are included in format.f and results displayed in format.out. Study them carefully and make suggested modifications to polish your skills.


Check your knowledge of this material, but first be sure your Web Browser works correctly.


Back to the Table of Contents / Home


Written and Maintained by John Mahaffy : jhm@psu.edu