FORMAT

Purpose

The FORMAT statement provides format specifications for input/output statements.

Syntax

Read syntax diagramSkip visual syntax diagram>>-FORMAT--(--+------------------+--)--------------------------><
              '-format_item_list-'
 

format_item
Read syntax diagramSkip visual syntax diagram>>-+-+---+--data_edit_desc---------+---------------------------><
   | '-r-'                         |
   +-control_edit_desc-------------+
   +-+---+--(--format_item_list--)-+
   | '-r-'                         |
   '-char_string_edit_desc---------'
 
r
is an unsigned, positive, integer literal constant that cannot specify a kind type parameter, or it is a scalar integer expression enclosed by angle brackets (< and >). It is called a repeat specification. It specifies the number of times to repeat the format_item_list or the data_edit_desc. The default is 1.
data_edit_desc
is a data edit descriptor
control_edit_desc
is a control edit descriptor
char_string_edit_desc
is a character string edit descriptor

Rules

When a format identifier in a formatted READ, WRITE, or PRINT statement is a statement label or a variable that is assigned a statement label, the statement label identifies a FORMAT statement.

The FORMAT statement must have a statement label. FORMAT statements cannot appear in block data program units, interface blocks, the scope of a module, or derived-type definitions.

Commas separate edit descriptors. You can omit the comma between a P edit descriptor and an F, E, EN, ES, D, G, or Q (both extended precision and character count) edit descriptor immediately following it, before a slash edit descriptor when the optional repeat specification is not present, after a slash edit descriptor, and before or after a colon edit descriptor.

FORMAT specifications can also be given as character expressions in input/output statements.

XL Fortran treats uppercase and lowercase characters in format specifications the same, except in character string edit descriptors.

Character format specification

When a format identifier (see READ) in a formatted READ, WRITE, or PRINT statement is a character array name or character expression, the value of the array or expression is a character format specification.

If the format identifier is a character array element name, the format specification must be completely contained within the array element. If the format identifier is a character array name, the format specification can continue beyond the first element into following consecutive elements.

Blanks can precede the format specification. Character data can follow the right parenthesis that ends the format specification without affecting the format specification.

IBM Extension

Variable format expressions

Wherever an integer constant is required by an edit descriptor, you can specify an integer expression in a FORMAT statement. The integer expression must be enclosed by angle brackets (< and >). You cannot use a sign outside of a variable format expression. The following are valid format specifications:

      WRITE(6,20) INT1
20    FORMAT(I<MAX(20,5)>)

      WRITE(6,FMT=30) INT2, INT3
30    FORMAT(I<J+K>,I<2*M>)

The integer expression can be any valid Fortran expression, including function calls and references to dummy arguments, with the following restrictions:

The value of the expression is reevaluated each time an input/output item is processed during the execution of the READ, WRITE, or PRINT statement.

End of IBM Extension

Examples

      CHARACTER*32 CHARVAR
      CHARVAR="('integer: ',I2,'  binary: ',B8)"  ! Character format
      M = 56                                      ! specification
      J = 1                                       !     OUTPUT:
      X = 2355.95843                              !
      WRITE (6,770) M,X                           !  56   2355.96
      WRITE (6,CHARVAR) M,M                       ! integer: 56
                                                  ! binary: 00111000
      WRITE (6,880) J,M                           !  1
                                                  ! 56
770   FORMAT(I3, 2F10.2)
880   FORMAT(I<J+1>)
      END

Related information