Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 39 ยท Language Basics

Reading and Writing Basics

Learn Fortran print, write and read statements, list-directed output, format descriptors I, F, ES, A and X, line control with slash, advance=no and reading numbers from the keyboard.

Programs need input and output. Fortran has three statements for it: print (to the screen), write (to the screen, a file or a string) and read (from the keyboard, a file or a string). Each can use list-directed I/O (the compiler chooses the layout) or formatted I/O (you control the layout with a format string). This lesson covers both for the screen and keyboard; files come in chapter 7.

You have been using print *, ... all along. The * means "list-directed": the compiler chooses the width and layout of each item, separated by spaces.

FORTRAN
program print_demo
  implicit none
  integer :: n = 42
  real :: x = 3.5
  character(len=5) :: s = "Hi"

  print *, n
  print *, x
  print *, "n =", n, "x =", x
  print *, s, "!"
end program print_demo

The first line starts with one blank, and numbers are right-aligned in a field whose width depends on the compiler. List-directed output is great for quick tests, but its exact spacing and digits are not standardized, so use formats when the layout matters.

write (*, *) items is the general form: the first * is the unit (the screen) and the second the format (list-directed). print *, items is shorthand for write (*, *) items.

Formatted output

A format string describes each item. Pass it as a string after the unit, in parentheses:

FORTRAN
print "(I5)", 42                 ! "   42"
print "(F8.3)", 3.14159          ! "   3.142"
print "(A)", "hello"             ! "hello"
write (*, "(A, I3, A, F6.2)") "n=", 7, " x=", 2.5

The most common edit descriptors:

Descriptor Meaning Example
Iw integer in w characters I5
Fw.d real, w wide, d decimals F8.3
ESw.d scientific notation ES12.4 gives 1.2346E+03
Aw or A character, width w or its length A10, A
Lw logical L1
nX skip n spaces 3X
/ start a new line /
Tn tab to column n T20

Rules worth remembering:

  • If a number does not fit the width, the field fills with asterisks: F4.1 printing 123.45 shows ****. Make the field wider.
  • Fw.d rounds: F8.2 with 3.14159 gives 3.14.
  • Use w = 0 (like I0 or F0.2) for the smallest width that fits. That is the easy way to remove padding.
  • A repeat count works: 3I4 means I4, I4, I4, and (2(F6.2, 1X)) repeats a group.
FORTRAN
print "(A, I0, A, F0.2)", "count=", 12, " mean=", 3.14159
! count=12 mean=3.14

The unit * with a character string format is the modern idiom. Old code puts the format on a format statement with a numeric label, which you will see in legacy programs:

FORTRAN
      WRITE (*, 100) N, X
  100 FORMAT (I5, F8.3)
Python Stand-in (Python): Fortran-style F, I and ES formats

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.

Controlling lines

Each print or write starts a new line. Use advance="no" to stay on the same line, which is handy for prompts:

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

A slash in a format inserts a line break, and a format with fewer items than the output list restarts on a new line ("format reversion"):

FORTRAN
print "(I3)", 1, 2, 3        ! three lines: "  1", "  2", "  3"
print "(3I3)", 1, 2, 3       ! one line: "  1  2  3"
print "(A, /, A)", "line 1", "line 2"
Python Stand-in (Python): format reversion

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.

Reading from the keyboard

read *, list reads values separated by blanks, commas or newlines, and it converts them to the type of each variable in the list:

FORTRAN
program read_demo
  implicit none
  integer :: n
  real :: x
  character(len=20) :: name

  print *, "Enter an integer, a real, and a name in quotes:"
  read *, n, x, name
  print "(A, I0, A, F0.2, 2A)", "n=", n, " x=", x, " name=", trim(name)
end program read_demo

For input 5 2.5 "Ada" it prints n=5 x=2.50 name=Ada. Notice list-directed read needs a quoted string if the text contains spaces; an unquoted word is fine for a single token. To read a whole line including spaces, use a format: read "(A)", line.

Detecting errors and end of input

A bad input (letters where a number is expected) normally stops the program with a run-time error. Capture it with iostat:

FORTRAN
integer :: ios
read (*, *, iostat=ios) n
if (ios /= 0) then
  print *, "That was not an integer."
end if

iostat is zero on success, positive on an error, and negative at the end of input. Error handling is covered again in the file I/O chapter; for now remember to always check ios for user input.

Python Stand-in (Python): iostat-style input check

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.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the format (F8.3) print for 3.14159?

    F8.3 is 8 characters wide with 3 decimals, rounded and right-aligned: three spaces then 3.142.
  2. 2. What appears when a number does not fit the field width?

    An overflowing field is filled with asterisks; widen it, or use w = 0 as in I0.
  3. 3. Which option keeps the cursor on the same line after write?

    advance="no" suppresses the end-of-record, which is how prompts are written.
  4. 4. What does a negative iostat value from read mean?

    iostat is 0 on success, positive for an error and negative at the end of the input.

Summary

  • print * and write (*, *) use list-directed output with compiler-chosen layout.
  • Formats such as "(I5, F8.3, A)" control layout; w = 0 gives the smallest width.
  • Too-narrow fields print asterisks; a slash starts a new line; advance="no" stays on the line.
  • read * converts keyboard input; strings with spaces need quotes or an A format.
  • Use iostat to detect bad input and end of input.

Next: The if Statement and Logical Expressions