A format tells read and write how to turn values into text and back. It is a parenthesised list of edit descriptors. It is written as a
format statement with a label, as a character expression (a literal, a variable or an array) whose text begins with (, or left out with * for list-directed input and output.
write (*, '(a, i5, f10.3)') 'n =', n, x
write (*, 100) n, x
100 format (1x, 'n =', i5, 3x, f10.3)
write (*, '(2(i4, ","), /, es12.4e3)') a, b, c, d
format statement, and with repeat counts, a record break and an exponent width.Data edit descriptors
w is the field width, d the digits after the decimal point, m the minimum number of digits and e the digits of the exponent. A repeat count may come first (3i5).
| Descriptor | Converts | Example (output) |
|---|---|---|
Iw, Iw.m | integer in decimal; m pads with leading zeros | I5 of 42 gives ···42; I5.3 gives ·042 |
Bw, Bw.m | integer in binary | B8 of 5 gives ·····101 |
Ow, Ow.m | integer in octal | O8 of 64 gives ·····100 |
Zw, Zw.m | integer in hexadecimal | Z8 of 255 gives ······FF |
Fw.d | real, fixed point | F8.3 of 3.14159 gives ···3.142 |
Ew.d, Ew.dEe | real with an exponent, mantissa between 0.1 and 1 | E12.4 of 12345.6 gives ··0.1235E+05 |
ENw.d, ENw.dEe | engineering notation: exponent a multiple of 3 | EN12.4 of 12345.6 gives ·12.3456E+03 |
ESw.d, ESw.dEe | scientific notation: mantissa from 1 to 10 | ES12.4 of 12345.6 gives ··1.2346E+04 |
EXw.d, EXw.dEe | hexadecimal significand with a binary exponent (Fortran 2018) | exact, round-trip output of a real |
Dw.d | real with a D exponent letter, as E otherwise (a FORTRAN 66 form for double precision) | D12.4 of 12345.6 gives ··0.1235D+05 |
Gw.d, Gw.dEe | general: like F for numbers of moderate size and like E otherwise; for an integer, logical or character value it acts as I, L or A (Fortran 90 and later). G0 (Fortran 2008) gives the shortest exact output | |
Lw | logical: T or F on output; T or F, .TRUE. and so on on input | L5 of .true. gives ····T |
A, Aw | character: no width takes the length of the value | A8 of 'abc' gives ·····abc (right-justified on output); on input the rightmost len characters of the field are taken if w is larger |
DT, DT'name', DT'name'(v-list) | derived type through its user-defined I/O procedure (Fortran 2003) |
When the field is too narrow for a value on output, the whole field is filled with *. The standard has no free-form widths, but w of 0 on I, B, O, Z and F (Fortran 95 and later) means “the least width that will do”:
I0 and F0.3 print with no padding. GNU Fortran (with -fdec-format-defaults) and Intel Fortran also accept F, G and I with no width.
On input, Fw.d reads a number that has its own decimal point as written and takes d only when there is none.
Control edit descriptors
| Descriptor | Effect |
|---|---|
nX | skip n positions (X alone is 1X in GNU Fortran as an extension) |
Tn | move to position n of the record |
TLn, TRn | move n positions left or right |
/ | end the record: start a new line on output, skip a record on input |
: | stop here if there are no more items in the list |
S, SP, SS | sign control: processor default, always print +, never print + |
kP | scale factor: 1PE12.4 shifts the mantissa one place. Stays in force for the rest of the format. The comma after P may be left out |
BN, BZ | blank control on input: ignore blanks in numeric fields, or read them as zeros |
RU, RD, RZ, RN, RC, RP | rounding mode: up, down, toward zero, nearest, compatible, processor default (Fortran 2003) |
DC, DP | decimal comma or decimal point (Fortran 2003) |
Character string and Hollerith descriptors
- a character literal (
'text'or"text") in the format is written as it stands on output, and is not allowed on input. The delimiter is doubled to put it in the text nHtextis the Hollerith form: a count,Hand exactly that many characters (5HHELLO). It was the only text form of FORTRAN 66, was kept in FORTRAN 77 and deleted in Fortran 95; compilers still accept it- the viewer colours literals and Hollerith strings in a
formatstatement as strings, and the descriptors as keywords. Formats written as character literals inreadandwriteare one string, because the highlighter marks one token per literal
Format lists, repeat counts and reversion
- descriptors are separated by commas, and the comma may be left out next to
/and:and after a scale factor 3I5repeats a data descriptor three times;2(I5, F8.2)repeats a group;*(I5)repeats a group without limit (Fortran 2008)- reversion: when the item list is longer than the format, a new record starts and the format is used again from its last top-level parenthesis (or the start when there are none); descriptors that follow the last item stop at the first data descriptor with no item (or at
:) - a format may be a character array: the elements are joined before it is read
Vendor descriptors and extensions
$or\: no new line after this output (DEC and Intel;advance='no'is the standard way)Q: the number of characters left in the input record (DEC)- widths left out of
F,G,I; a comma after the last item; a missing period inFw(GNU Fortran accepts these, see its manual’s “Extensions” chapter)