FORMAT

Statement: Specifies the form of data being transferred and the data conversion (editing) required to achieve that form.

Syntax

FORMAT (format-list)

format-list
Is a list of one or more of the following edit descriptors, separated by commas or slashes (/):
Data edit descriptors: I, B, O, Z, F, E, EN, ES, D, G, L, and A.
Control edit descriptors: T, TL, TR, X, S, SP, SS, BN, BZ, P, :, /, $, \, and Q.
String edit descriptors: H, 'c', and "c", where c is a character constant.

A comma can be omitted in the following cases:

Edit descriptors can be nested and a repeat specification can precede data edit descriptors, the slash edit descriptor, or a parenthesized list of edit descriptors.

Rules and Behavior

A FORMAT statement must be labeled.

Named constants are not permitted in format specifications.

If the associated I/O statement contains an I/O list, the format specification must contain at least one data edit descriptor or the control edit descriptor Q.

Blank characters can precede the initial left parenthesis, and additional blanks can appear anywhere within the format specification. These blanks have no meaning unless they are within a character string edit descriptor.

When a formatted input statement is executed, the setting of the BLANK specifier (for the relevant logical unit) determines the interpretation of blanks within the specification. If the BN or BZ edit descriptors are specified for a formatted input statement, they supersede the default interpretation of blanks. (For more information on BLANK defaults, see the OPEN statement.

For formatted input, use the comma as an external field separator. The comma terminates the input of fields (for noncharacter data types) that are shorter than the number of characters expected. It can also designate null (zero-length) fields.

The first character of a record transmitted to a line printer or terminal is typically used for carriage control; it is not printed. The first character of such a record should be a blank, 0, 1, $, +, or ASCII NUL. Any other character is treated as a blank.

A format specification cannot specify more output characters than the external record can contain. For example, a line printer record cannot contain more than 133 characters, including the carriage control character.

Whenever an edit descriptor requires an integer constant, you can specify an integer expression in a FORMAT statement. The integer expression must be enclosed by angle brackets (< and >). The following examples 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.

The following table summarizes the edit descriptors:

Data Edit Descriptors
Code  Form 1 Effect 
A[w]  Transfers character or Hollerith values. 
Bw[.m]  Transfers binary values. 
Dw.d  Transfers real values with D exponents.  
Ew.d[Ee]  Transfers real values with E exponents.  
EN  ENw.d[Ee]  Transfers real values with engineering notation.  
ES  ESw.d[Ee]  Transfers real values with scientific notation.  
Fw.d  Transfers real values with no exponent.  
Gw.d[Ee]  Transfers values of all intrinsic types.  
Iw[.m]  Transfers decimal integer values.  
Lw  Transfers logical values: on input, transfers characters; on output, transfers T or F. 
Ow[.m]  Transfers octal values. 
Zw[.m]  Transfers hexadecimal values. 
1 w is the field width
  m is the minimum number of digits that must be in the field (including zeros).
  d is the number of digits to the right of the decimal point
  E is the exponent field
  e is the number of digits in the exponent
Control Edit Descriptors
Code  Form  Effect 
BN  BN  Ignores embedded and trailing blanks in a numeric input field. 
BZ  BZ  Treats embedded and trailing blanks in a numeric input field as zeros. 
kP  Interprets certain real numbers with a specified scale factor.  
Q  Q   Returns the number of characters remaining in an input record. 
Reinvokes optional plus sign (+) in numeric output fields; counters the action of SP and SS. 
SP  SP  Writes optional plus sign (+) into numeric output fields. 
SS  SS  Suppresses optional plus sign (+) in numeric output fields. 
Tn  Tabs to specified position. 
TL  TLn  Tabs left the specified number of positions.  
TR  TRn  Tabs right the specified number of positions.  
nX  Skips the specified number of positions. 
$  $   Suppresses trailing carriage return during interactive I/O.  
Terminates format control if there are no more items in the I/O list. 
[r]/  Terminates the current record and moves to the next record. 
\  \   Continues the same record; same as $
String Edit Descriptors
Code  Form  Effect 
nHch[ch...]  Transfers characters following the H edit descriptor to an output record. 
'c' 2   'c' Transfers the character literal constant (between the delimiters) to an output record. 
2 These delimiters can also be quotation marks (").

Compatibility

CONSOLE STANDARD GRAPHICS QUICKWIN GRAPHICS WINDOWS DLL LIB

See Also: I/O Formatting, Format Specifications, Data Edit Descriptors

Example

   INTEGER width, value
   width = 2
   read (*,1) width, value
!  if the input is 3123, prints 123, not 12
1  format ( i1, i<width>)
   print *, value
   END