Formatted output with I, F, E, A, L, X

A format is a string of edit descriptors, and each descriptor says how one value is written: how wide the field is, how many digits come after the decimal point, whether an exponent is shown. A report is a format and a print per row, and the widths are what make the columns line up. This lesson writes one descriptor at a time and then a whole table.

The descriptors

i5 is an integer in five columns, f7.2 a real with two decimals in seven columns, e12.3 a real in exponent form, a8 a string of eight characters, l3 a logical, and nx n blanks. A string is right-aligned in its field unless the descriptor is a without a width, and a number is always right-aligned.

The file descriptors.f90 prints one line per descriptor with a bracket after it, so the width is visible:

program descriptors_demo implicit none print '(a)', "descriptor output" print '(a,3x,a)', "X :", "[ ]" print '(a,i5,a)', "I5 :", 42, "|" print '(a,f7.2,a)', "F7.2:", 3.14159, "|" print '(a,e12.3,a)', "E12 :", 1234.5, "|" print '(a,a8,a)', "A8 :", "right", "|" print '(a,l3,a)', "L3 :", .true., "|" print '(a,i0)', "I0 :", 42 end program descriptors_demo

gfortran -Wall -Wextra -std=f2018 descriptors.f90 -o descriptors
./descriptors

descriptor output
X : [ ]
I5 : 42|
F7.2: 3.14|
E12 : 0.123E+04|
A8 : right|
L3 : T|
I0 :42

Read the widths: the five columns of i5 hold three blanks and 42, the seven of f7.2 hold two blanks, 3.14, and rounding dropped the rest, and the twelve of e12.3 hold the mantissa and the exponent. i0 is the minimum width the value needs, which is why 42 has no blanks at all.

A table that lines up

A table is one format for the header and one for the rows, with the widths chosen so that every column has the same place in both. The title row uses the a descriptor with the same widths as the data below it.

program table_demo implicit none print '(a10,a6,a7,a10,a5)', "name", "count", "price", "total", "ok" print '(a10,i6,f7.2,f10.2,l5)', "widget", 12, 3.5, 42.0, .true. print '(a10,i6,f7.2,f10.2,l5)', "gadget", 3, 12.25, 36.75, .false. print '(a10,i6,f7.2,f10.2,l5)', "long name", 100, 0.5, 50.0, .true. end program table_demo

gfortran -Wall -Wextra -std=f2018 table.f90 -o table
./table

name count price total ok
widget 12 3.50 42.00 T
gadget 3 12.25 36.75 F
long name 100 0.50 50.00 T

The header is right-aligned because a10 right-aligns like every other descriptor; a report head is usually better left-aligned, and adjustl inside the print, or a t descriptor, moves it. The data rows line up with each other exactly, which is the part that matters for reading numbers.

The format may be written as a character expression, so a report can compute its widths: write (fmt, '(a,i0,a)') "(a", n, ")" builds a format string at run time. The building lesson wrote one into a character variable already.

F, E, ES and G

f writes a real in fixed notation with a fixed number of decimals, which is right for prices and wrong for anything whose magnitude varies: a value below the smallest printable decimal prints as zeros. e writes a normalised mantissa in the range 0 to 1, es a mantissa in the range 1 to 10, and g picks fixed or exponent notation by the magnitude.

program exponent_demo implicit none print '(a,f12.4)', "F 1234.5 :", 1234.5 print '(a,f12.4)', "F 0.00012345:", 0.00012345 print '(a,es12.4)', "ES 1234.5 :", 1234.5 print '(a,es12.4)', "ES 0.00012345:", 0.00012345 print '(a,g12.4)', "G 1234.5 :", 1234.5 end program exponent_demo

gfortran -Wall -Wextra -std=f2018 exponent.f90 -o exponent
./exponent

F 1234.5 : 1234.5000
F 0.00012345: 0.0001
ES 1234.5 : 1.2345E+03
ES 0.00012345: 1.2345E-04
G 1234.5 : 1234.

The f line with four decimals turns 0.00012345 into 0.0001: three of the four decimals are leading zeros, and the number lost three digits of information while looking perfectly formatted. The es form keeps them, and it is what a numerical report should print for quantities with an unknown scale.

A field that is too narrow

A value that does not fit is not truncated silently: a number fills its whole field with asterisks, and a string keeps as much as fits. The asterisks are the compiler telling the program that its width was wrong.

program narrow_demo implicit none print '(a,i3,a)', "[", 12345, "]" print '(a,f5.2,a)', "[", 1234.5, "]" print '(a,a4,a)', "[", "longer", "]" end program narrow_demo

gfortran -Wall -Wextra -std=f2018 narrow.f90 -o narrow
./narrow

[***]
[*****]
[long]

Two width mistakes, two outcomes: the numbers become stars and the string is cut. A width computed from the data must therefore allow for the largest value, and a report that prints stars has to widen the column rather than print a smaller number.

Exercises

  1. Print 42 with i3, i5 and i0 and count the blanks around it.
  2. Add a row to the table with a name of exactly ten characters. Does the column stay aligned?
  3. Print 0.00012345 with es10.4 and with f10.6. Which one keeps the digits?
  4. Print the value 123456 with i5. What comes out, and what does it say about the width you chose?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does the descriptor i5 print for 42?

    Answer

    Three blanks and 42 in a field of five

    An integer is right-aligned in its field.

  2. What does f7.2 mean?

    Answer

    A real in a field of seven with two digits after the point

    The number before the dot is the width and the one after is the decimals.

  3. What does this program print? program p implicit none print "(a,i4,a)", "[", 99, "]" end program p

    Answer

    [ 99]

    Two blanks and two digits fill the four columns.

  4. Which descriptor prints a value in exponent notation with a mantissa between one and ten?

    Answer

    ES

    ES normalises the mantissa to 1 through 10; E uses 0 through 1.

  5. What does a number that does not fit its field print?

    Answer

    A field full of asterisks

    Asterisks mark an overflow of the width.

  6. What does i0 do?

    Answer

    Uses the minimum width the value needs

    The zero width means "as narrow as possible".

  7. Why is f the wrong descriptor for a value whose magnitude varies?

    Answer

    A fixed number of decimals loses digits or shows leading zeros

    Fixed notation has a fixed resolution and a fixed range.

  8. What does the a8 descriptor do with a five-character string?

    Answer

    Right-aligns it with three blanks in front

    A is right-aligned like the numeric descriptors.

  9. How is a header row made to line up with the data rows?

    Answer

    Use the same widths in the header format as in the data format

    Column positions come from the widths, so they must match.

  10. What does the l3 descriptor print for .TRUE.?

    Answer

    Two blanks and a T

    A logical prints as T or F, right-aligned in its field.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image