Format Specifications

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

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

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.

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.  
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.  
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 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.

In the following example, the integer-variable name MYFMT refers to the FORMAT statement 9000, as assigned just before the FORMAT statement.

       ASSIGN 9000 TO MYFMT
 9000  FORMAT (I5, 3F4.5, A16)
 !  I5, 3F5.2, A16 is the format list.
       WRITE (*, MYFMT) iolist

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

For More Information: