11.1 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 Section 12.6.4 for details on OpenVMS systems, or Section 13.6.4 for details on Tru64 UNIX, Linux, and Windows systems.)

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.

Table 11-1 summarizes the edit descriptors that can be used in format specifications.

Table 11-1 Summary of Edit Descriptors

Code  Form  Effect   
A[w]  Transfers character or Hollerith values.  (Section 11.2.6
Bw[.m]  Transfers binary values.  (Section 11.2.3.2
BN  BN  Ignores embedded and trailing blanks in a numeric input field.  (Section 11.3.4.1
BZ  BZ  Treats embedded and trailing blanks in a numeric input field as zeros.  (Section 11.3.4.2
Dw.d  Transfers real values with D exponents.   (Section 11.2.4.2
Ew.d[Ee]  Transfers real values with E exponents.   (Section 11.2.4.2
EN  ENw.d[Ee]  Transfers real values with engineering notation.   (Section 11.2.4.3
ES  ESw.d[Ee]  Transfers real values with scientific notation.   (Section 11.2.4.4
Fw.d  Transfers real values with no exponent.   (Section 11.2.4.1
Gw.d[Ee]  Transfers values of all intrinsic types.   (Section 11.2.4.5
nHch[ch...]  Transfers characters following the H edit descriptor to an output record.  (Section 11.4
Iw[.m]  Transfers decimal integer values.   (Section 11.2.3.1
Lw  Transfers logical values: on input, transfers characters; on output, transfers T or F.  (Section 11.2.5
Ow[.m]  Transfers octal values.  (Section 11.2.3.3
kP  Interprets certain real numbers with a specified scale factor.   (Section 11.3.5
Q  Q   Returns the number of characters remaining in an input record.  (Section 11.3.9
Reinvokes optional plus sign (+) in numeric output fields; counters the action of SP and SS.  (Section 11.3.3.3
SP  SP  Writes optional plus sign (+) into numeric output fields.  (Section 11.3.3.1
SS  SS  Suppresses optional plus sign (+) in numeric output fields.  (Section 11.3.3.2
Tn  Tabs to specified position.  (Section 11.3.2.1
TL  TLn  Tabs left the specified number of positions.   (Section 11.3.2.2
TR  TRn  Tabs right the specified number of positions.   (Section 11.3.2.3
nX  Skips the specified number of positions.  (Section 11.3.2.4
Zw[.m]  Transfers hexadecimal values.  (Section 11.2.3.4
$  $   Suppresses trailing carriage return during interactive I/O.   (Section 11.3.8
Terminates format control if there are no more items in the I/O list.  (Section 11.3.7
[r]/  Terminates the current record and moves to the next record.  (Section 11.3.6
\  \   Continues the same record; same as $ (Section 11.3.8
'c'1   'c'  Transfers the character literal constant (between the delimiters) to an output record.  (Section 11.4
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.

For More Information:


Previous Page Next Page Table of Contents