Purpose
The FORMAT statement provides format specifications for input/output statements.
Format
>>-FORMAT--(--+------------------+--)-------------------------->< '-format_item_list-' |
>>-+-+---+--data_edit_desc---------+--------------------------->< | '-r-' | +-control_edit_desc-------------+ +-+---+--(--format_item_list--)-+ | '-r-' | '-char_string_edit_desc---------' |
Forms | Use | Page |
---|---|---|
A Aw | Edits character values | "A (Character) Editing" |
Bw Bw.m | Edits binary values | "B (Binary) Editing" |
Ew.d Ew.dEe Ew.dDe * Ew.dQe * Dw.d ENw.d ENw.dEe ESw.d ESw.dEe Qw.d * | Edits real and complex numbers with exponents | "E, D, and Q (Extended Precision) Editing" |
Fw.d | Edits real and complex numbers without exponents | "F (Real without Exponent) Editing" |
Gw.d Gw.dEe Gw.dDe * Gw.dQe * | Edits data fields of any intrinsic type, with the output format adapting to the type of the data and, if the data is of type real, the magnitude of the data | "G (General) Editing" |
Iw Iw.m | Edits integer numbers | "I (Integer) Editing" |
Lw | Edits logical values | "L (Logical) Editing" |
Ow Ow.m | Edits octal values | "O (Octal) Editing" |
Q * | Returns the count of characters remaining in an input record * | "Q (Character Count) Editing" |
Zw Zw.m | Edits hexadecimal values | "Z (Hexadecimal) Editing" |
where:
w, m, d, and e can be:
+-------------------------------IBM Extension--------------------------------+
+----------------------------End of IBM Extension----------------------------+
You cannot specify kind parameters for w, m, d, or e.
+-------------------------------IBM Extension--------------------------------+
There are two types of Q data edit descriptor (Qw.d and Q):
+----------------------------End of IBM Extension----------------------------+
Forms | Use | Page |
---|---|---|
/ r / | Specifies the end of data transfer on the current record | "/ (Slash) Editing" |
: | Specifies the end of format control if there are no more items in the input/output list | ": (Colon) Editing" |
$ * | Suppresses end-of-record in output * | "$ (Dollar) Editing" * |
BN | Ignores nonleading blanks in numeric input fields | "BN (Blank Null) and BZ (Blank Zero) Editing" |
BZ | Interprets nonleading blanks in numeric input fields as zeros | "BN (Blank Null) and BZ (Blank Zero) Editing" |
kP | Specifies a scale factor for real and complex items | "P (Scale Factor) Editing" |
S SS | Specifies that plus signs are not to be written | "S, SP, and SS (Sign Control) Editing" |
SP | Specifies that plus signs are to be written | "S, SP, and SS (Sign Control) Editing" |
Tc | Specifies the absolute position in a record from which, or to which, the next character is transferred | "T, TL, TR, and X (Positional) Editing" |
TLc | Specifies the relative position (backward from the current position in a record) from which, or to which, the next character is transferred | "T, TL, TR, and X (Positional) Editing" |
TRc | Specifies the relative position (forward from the current position in a record) from which, or to which, the next character is transferred | "T, TL, TR, and X (Positional) Editing" |
oX | Specifies the relative position (forward from the current position in a record) from which, or to which, the next character is transferred | "T, TL, TR, and X (Positional) Editing" |
where:
+-------------------------------IBM Extension--------------------------------+
r, k, c, and o can also be expressed as an arithmetic expression enclosed by angle brackets (< and >) that evaluates into an integer value.
+----------------------------End of IBM Extension----------------------------+
Kind type parameters cannot be specified for r, k, c, or o.
Forms | Use | Page |
---|---|---|
nHstr | Outputs a character string (str) | "H Editing" |
'str' "str" | Outputs a character string (str) | "Apostrophe/Double Quotation Mark Editing (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.
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--------------------------------+
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----------------------------+
CHARACTER*32 CHARVAR CHARVAR="('integer: ',I2,' binary: ',B8)" ! Character format specification M = 56 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