Format Statement

Statement Type:

Format is an executable statement

Statement Purpose:

This command is used to tell the computer specifically how you want your output to be displayed or how to read it. It must have a line label number before it. This is because a format statement needs to be referenced by a READ or WRITE statement. Even though format statements can be placed anywhere in the program unit using them, it is generally recommended that you put them at the end of in program unit ( main program, subroutine, function) in which they are used, if they are used by more than one READ or WRITE statement.

Required Form:

The general form of a format statement is as follows:
		2000 format ( edit descriptors )
As you can see from above, the only two additional things that must be included with a format statement are a line label number and a string of edit descriptors. The line label number is necessary so that the referencing READ or WRITE statement knows were to go to pick up the proper format information. The edit descriptors are what tell the compiler how the data is supposed to be input or output. A list describing each individual edit descriptor can be found in the Optional Descriptors Section.

Optional Descriptors:

EDIT DESCRIPTORS:

  1. I or i - This letter followed by number tells the computer to either read the next number of characters as integers or to use the next number of spaces for an integer when dealing with output.
  2. F or f - Used in the following form: f5.2. This means to read or output the next five characters as real numbers with space for two digits after the decimal point.
  3. E or e -It is used for numbers in Scientific notation. Its format is the same as for the f edit descriptor
  4. A or a -When this is used followed by a number it means to read the next number of characters as characters. When a plan a is used, it means to just read (write) however many characters are necessary to match the length of the corresponding character variable in the READ (or WRITE) statement.
  5. X or x - A number is placed before the x. This will tell the computer to skip or ignore the next number of characters on input or insert that many blanks on output.
  6. T or t - A number after the t will tell the computer that the next character to be read or output will begin at the position corresponding to the number
  7. TL and TR- A number is typed after both of these when they are used. TL means the next character to be read or output will be a certain number of spaces to the left of the current position in the line. TR means the next character to be read or output will be located a certain number of spaces to the right.
  8. P or p - This is used in output. The number placed in front of the p will indicate how many significant digits you want to appear before the decimal point with the exponential (E) format. Default is 0p.

Examples:

To output a 3 by 3 matrix called data
  	    write (*,2000)((a(i,j),j=1,3),i=1,3)
    2000 format ((1x,3(f5.2,2x)))
Note on parenthesis: When they are used in a format statement the computer will loop through the entire thing once. Then if there is additional information left to be output (or input), it 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. Each wrap around in the use of a FORMAT, causes a new line to be started. Also, the "3" that appears before the last set of imbedded parenthesis, tells the compiler to loop through the edit descriptors embedded in those parentheses three times without forcing a new line on each pass. In this example the presence of this repeat count "3" causes the wrap after output of elements a(1,1) a(1,2) and a(1,3) to go to the edit descriptor "1x", beginning the next line with one space before printing a(2,1), a(2,2) and a(2,3). Note on the write statement: If you look at the write statement you will notice the two implied do loops in it. For further information on IMPLIED DO loops see the statement definitions.

Programs where additional examples can be found:

array1.f, array2.f and format.f

Related Topics and lectures:

write statement

read statement

lectures eighteen, twenty two, twenty three

Up one level / Home


Written by Jason Wehr : jcw142@psu.edu and Maintained by John Mahaffy : jhm@cac.psu.edu