Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 39 ยท File I/O and Formatting

Formatted Output and Edit Descriptors in Fortran

Control Fortran output with format specifiers: I, F, E, ES, A, L, X and slash descriptors, repeat counts, reversion, internal writes and tidy tables of numbers.

print *, x is list-directed output: quick, but the compiler decides the layout, and the result differs between compilers. For reports, tables and files that other programs read, you control the layout yourself with a format: a list of edit descriptors that say how each value is written. Fortran's format language is compact and powerful, and numerical programmers use it constantly.

A format is a character string, usually in parentheses, passed to write or print:

FORTRAN
write (*, "(f8.3)") 3.14159
print "(i5)", 42

The first argument of write is the unit (* is the screen) and the second is the format.

Integer, real and text descriptors

Descriptor Meaning Example
Iw integer in w columns i5 writes 42 as 42
Iw.m integer, at least m digits i5.3 writes 7 as 007
Fw.d fixed-point real, d decimals f8.3 writes 3.14159 as 3.142
Ew.d exponent form e12.4 writes 12345.678 as 0.1235E+05
ESw.d scientific form es12.4 writes 12345.678 as 1.2346E+04
Aw / A character, width w or the string's length a writes the string as is
Lw logical l1 writes T or F
nX skip n columns (blank) 3x
/ start a new line /

Values are right-justified in their field, and if a number does not fit it prints as a row of asterisks (********). That is your cue to widen the field. A width of 0 (i0, f0.3) means "use as many columns as needed", but note that f0.3 of 0.5 may print .500 without a leading zero.

Python Stand-in: the same layouts in Python

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python's e format puts a leading digit before the point like ES, not like Fortran's E, which starts with 0.. Keep that in mind when checking numbers across languages.

Repeat counts, groups and reversion

Descriptors can be repeated and grouped:

FORTRAN
write (*, "(3i4)") 1, 2, 3                 ! 3 integers, width 4 each
write (*, "(a, 2f8.2)") "x y:", 1.5, 2.25
write (*, "(2(i3, 1x, f6.1))") 1, 2.5, 2, 3.5

If you write more values than the format has descriptors, the format is reused from the start of the last parenthesized group, on a new line. This reversion is the standard way to print a whole array in columns:

FORTRAN
integer :: a(10) = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
write (*, "(5i4)") a

That writes two lines of five numbers: 1 2 3 4 5 then 6 7 8 9 10. For a runtime-dependent count, build the format with an internal write (below) or use the * repeat shortcut (*(i4)), which says "repeat the group as long as values remain".

FORTRAN
write (*, "(*(f6.2))") x       ! all of x on one line
write (*, "(*(i0, ','))") a    ! 1,2,3,...,10,   (trailing comma)

Strings

A alone prints a string at its natural length; Aw right-justifies in w columns (and truncates from the right if w is shorter than the text). Quoted literals can sit directly in a format:

FORTRAN
write (*, "('Total: ', f10.2, ' EUR')") total
write (*, "(a, ':', t12, f8.3)") name, value   ! t12 = tab to column 12

T (tab to absolute column), TL/TR (relative left/right) and X position the output precisely, which is how aligned tables are built.

Common descriptors

Iw Integers
Fw.d / ESw.d Fixed and scientific reals
A Text
/ and X Newlines and spaces

Too narrow a field prints asterisks

1 / 2

No newline? advance="no"

Each write ends the line. Suppress that for prompts and progress dots:

FORTRAN
write (*, "(a)", advance="no") "Enter a number: "
read (*, *) n

Internal files: write to a string

The "unit" in a write can be a character variable. This is how you convert numbers to text, or build a format at run time:

FORTRAN
character(len=32) :: text
character(len=16) :: fmt
integer :: n

write (text, "(f6.2)") 3.14159      ! text now holds "  3.14"
n = 4
write (fmt, "('(', i0, 'f8.3)')") n   ! fmt = "(4f8.3)"
write (*, fmt) 1.0, 2.0, 3.0, 4.0

Reading works the same way: read (text, *) x converts text to a number, with iostat= to detect bad input (see file I/O basics).

Printing a table

FORTRAN
program table
  implicit none
  integer :: i
  write (*, "(a4, a10, a12)") "n", "n**2", "sqrt(n)"
  do i = 1, 5
    write (*, "(i4, i10, f12.5)") i, i**2, sqrt(real(i))
  end do
end program table

The output is a header and five right-aligned rows. Every numeric column has a fixed width, so the decimals line up, which is the whole point of formats.

Python Stand-in: build the same table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Format statements and labels

Older code puts the format on a labelled line:

FORTRAN
write (*, 100) x, y
100 format (f8.3, 2x, f8.3)

It works and you will meet it, but a string literal next to the write is easier to read.

Common mistakes

  • A field too narrow: you see *****. Count the sign, decimal point and exponent.
  • A mismatch between descriptor and type (f8.3 with an integer) is a runtime error.
  • Forgetting that E output begins 0., and that ES is the scientific style.
  • Expecting a trailing newline to be missing: each write ends a line unless advance="no".
  • Reversion surprising you: extra values start a new line.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens when a number does not fit its field width?

    Fortran prints asterisks so you notice and widen the field.
  2. 2. What does the descriptor f8.3 print for 3.14159?

    Eight columns, three decimals, right-justified: three spaces, then 3.142.
  3. 3. How can you write a whole array on one line?

    The *( ) group repeats as long as values remain.
  4. 4. How do you build a character string from a number?

    Use a character variable as the unit in write: write (text, '(f6.2)') x.

Summary

  • Formats give exact control: I, F, E/ES, A, L, X, T and /.
  • Values are right-justified; too-narrow fields print asterisks.
  • Groups and repeat counts shorten formats; reversion reuses a format for extra values, *( ) repeats as needed.
  • advance="no" keeps the cursor on the line; internal writes convert between text and numbers.

Next: File I/O basics