# Format Specifications

A format specification can appear in a FORMAT statement or a character expression. In a FORMAT statement, it is preceded by the keyword FORMAT. A format specification takes one of the following forms:

( [format-items])

( [format-items, ] unlimited-format-item)

format-items | Is format-items [[,] format-item]... where format-item is one of the following:
[r] data-edit-desc
control-edit-desc
char-string-edit-desc
[r] (format-items)

r
  (Optional) Is an integer literal constant. This is called a repeat specification. The range of r is 1 through 2147483647 (2**31-1). If r is omitted, it is assumed to be 1.

data-edit-desc

  Is one of the data edit descriptors: I, B, O, Z, F, E, EN, ES, EX, D, DT, G, L, or A.

  A repeat specification can precede any data edit descriptor.

control-edit-desc

  Is one of the control edit descriptors: T, TL, TR, X, S, SP, SS, BN, BZ, P, :, /, $, \, and Q.

  A repeat specification can precede the slash (/) edit descriptor.

char-string-edit-desc
  Is one of the string edit descriptors: H, 'c', and "c", where c is a character constant.

unlimited-format-item
  Is * (format-items). The * indicates an unlimited repeat count.

If more than one edit descriptor is specified, they must be separated by commas or slashes (/).

A comma can be omitted in the following cases:

- Between a P edit descriptor and an immediately following F, E, EN, ES, EX, D, or G edit descriptor

- Before a slash (/) edit descriptor when the optional repeat specification is not present

- After a slash (/) edit descriptor

- Before or after a colon (:) edit descriptor

## Description

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 BLANK Specifier in OPEN statements.)

For formatted input, you can 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 following table summarizes the edit descriptors that can be used in format specifications.

Summary of Edit Descriptors
Code | Form | Effect
--- | --- | ---
A | A[w] | Transfers character or Hollerith values.
B | Bw[.m] | Transfers binary values.
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.
D | Dw.d | Transfers real values with D exponents.
DT | DT [string] [(v-list)] | Passes a character string and an integer array to a defined I/O procedure.
E | 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.
EX | EXw.d[Ee] | Transfers real values with hexadecimal-significands.
F | Fw.d | Transfers real values with no exponent.
G | Gw.d[Ee] | Transfers values of all intrinsic types.
H | nHch[ch...] | Transfers characters following the H edit descriptor to an output record.
I | Iw[.m] | Transfers decimal integer values.
L | Lw | Transfers logical values: on input, transfers characters; on output, transfers T or F.
O | Ow[.m] | Transfers octal values.
P | kP | Interprets certain real numbers with a specified scale factor.
Q | Q | Returns the number of characters remaining in an input record.
S | S | 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.
T | Tn | Tabs to specified position.
TL | TLn | Tabs left the specified number of positions.
TR | TRn | Tabs right the specified number of positions.
X | nX | Skips the specified number of positions.
Z | Zw[.m] | Transfers hexadecimal values.
$ | $ | 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 $.
'c'1 | 'c' | Transfers the character literal constant (between the delimiters) to an output record.
1 These delimiters can also be quotation marks (").

## Character Format Specifications

In data transfer I/O statements, a format specifier ([FMT=]format) can be a character expression that is a character array, character array element, or character constant. This type of format is also called a run-time format because it can be constructed or altered during program execution.

The expression must evaluate to a character string whose leading part is a valid format specification (including the enclosing parentheses).

If the expression is a character array element, the format specification must be contained entirely within that element.

If the expression is a character array, the format specification can continue past the first element into subsequent consecutive elements.

If the expression is a character constant delimited by apostrophes, use two consecutive apostrophes (`''` ) to represent an apostrophe character in the format specification; for example:

      PRINT '("NUM can''t be a real number")'

Similarly, if the expression is a character constant delimited by quotation marks, use two consecutive quotation marks ("") to represent a quotation mark character in the format specification.

To avoid using consecutive apostrophes or quotation marks, you can put the character constant in an I/O list instead of a format specification, as follows:

      PRINT "(A)", "NUM can't be a real number"

The following shows another character format specification:

      WRITE (6, '(I12, I4, I12)') I, J, K

In the following example, the format specification changes with each iteration of the DO loop:

    SUBROUTINE PRINT(TABLE)
    REAL TABLE(10,5)
    CHARACTER*5 FORCHR(0:5), RPAR*1, FBIG, FMED, FSML
    DATA FORCHR(0),RPAR /'(',')'/
    DATA FBIG,FMED,FSML /'F8.2,','F9.4,','F9.6,'/
    DO I=1,10
      DO J=1,5
        IF (TABLE(I,J) .GE. 100.) THEN
          FORCHR(J) = FBIG
        ELSE IF (TABLE(I,J) .GT. 0.1) THEN
          FORCHR(J) = FMED
        ELSE
          FORCHR(J) = FSML
        END IF
      END DO
      FORCHR(5)(5:5) = RPAR
      WRITE (6,FORCHR) (TABLE(I,J), J=1,5)
    END DO
    END

The DATA statement assigns a left parenthesis to character array element FORCHR(0), and (for later use) a right parenthesis and three F edit descriptors to character variables.

Next, the proper F edit descriptors are selected for inclusion in the format specification. The selection is based on the magnitude of the individual elements of array TABLE.

A right parenthesis is added to the format specification just before the WRITE statement uses it.

### Note

Format specifications stored in arrays are recompiled at run time each time they are used. If a Hollerith or character run-time format is used in a READ statement to read data into the format itself, that data is not copied back into the original array, and the array is unavailable for subsequent use as a run-time format specification.

## Examples

The following example shows a format specification:

            WRITE (*, 9000) int1, real1(3), char1
     9000   FORMAT (I5, 3F4.5, A16)
     !  I5, 3F5.2, A16 is the format list.

The following shows a format example using a character expression:

            WRITE (*, '(I5, 3F5.2, A16)')iolist
     !  I5, 3F4.5, A16 is the format list.

In the following example, the format list is put into an 80-character variable called MYLIST:

     CHARACTER(80) MYLIST
     MYLIST = '(I5, 3F5.2, A16)'
     WRITE (*, MYLIST) iolist

Consider the following two-dimensional array:

        1  2  3
        4  5  6

In this case, the elements are stored in memory in the order: 1, 4, 2, 5, 3, 6 as follows:

     CHARACTER(6) array(3)
     DATA array / '(I5', ',3F5.2', ',A16)' /
     WRITE (*, array) iolist

In the following example, the WRITE statement uses the character array element array(2) as the format specifier for data transfer:

     CHARACTER(80) array(5)
     array(2) = '(I5, 3F5.2, A16)'
     WRITE (*, array(2)) iolist

Parent topic: I/O Formatting

## See Also

Data edit descriptors
Control edit descriptors
Character string edit descriptors
Variable Format Expressions
Nested and group repeats
Printing of formatted records