Fortran Output Basics
Fortran has two output statements: print and write. Both take a format that controls the layout and a list of values to output. This lesson shows the basics; the next lesson explains format descriptors in detail.
print and write
program print_write
implicit none
integer :: steps = 250
print *, 'Steps:', steps ! list-directed, to the screen
write(*, *) 'Steps:', steps ! exactly the same
print '(a, i0)', 'Steps: ', steps ! with a format
write(*, '(a, i0)') 'Steps: ', steps ! the same with write
end program print_write Steps: 250
Steps: 250
Steps: 250
Steps: 250
print fmt, list always writes to standard output. write(unit, fmt) list is more general: the first item in the parentheses is the unit (where to write), the second is the format. * as a unit means standard output, and * as a format means list-directed.
| Statement | Destination | Format |
|---|---|---|
print *, x |
standard output | list-directed |
print '(f6.2)', x |
standard output | explicit |
write(*, *) x |
standard output | list-directed |
write(u, '(f6.2)') x |
unit u (a file, a string, stderr…) |
explicit |
List-directed output: convenient but compiler-specific
List-directed output (* format) picks widths for you. That is handy for debugging, but the exact spacing is not defined by the standard: gfortran, Intel and NAG all produce different layouts, and even the number of digits shown for a real differs.
program list_directed
use iso_fortran_env, only: real64
implicit none
print *, 1, 22, 333
print *, 1.5, 1.5_real64
print *, 'text', 42, .true., (1.0, -1.0)
end program list_directed 1 22 333
1.50000000 1.5000000000000000
text 42 T (1.00000000,-1.00000000)
Use list-directed output for quick experiments, and explicit formats for results people read or that other programs parse. From the next lesson on, this course mostly uses formats.
Standard output and standard error
The iso_fortran_env module names the standard units: output_unit (normally 6), error_unit (normally 0) and input_unit (normally 5). Error messages belong on error_unit so that they don't mix with data when output is redirected to a file.
program units
use iso_fortran_env, only: output_unit, error_unit
implicit none
write(output_unit, '(a)') 'Result: 42'
write(error_unit, '(a)') 'Warning: tolerance not reached'
end program unitsResult: 42
Warning: tolerance not reached
(Here both streams are shown together; in a terminal you could separate them with ./prog > results.txt.)
Staying on the same line
Each print or write normally starts a new line. A write with advance='no' leaves the cursor where it is, so the next output continues on the same line. This only works with an explicit format, not with *.
program progress
implicit none
integer :: i
write(*, '(a)', advance='no') 'Loading'
do i = 1, 5
write(*, '(a)', advance='no') '.'
end do
write(*, '(a)') ' done'
end program progressLoading..... done
Printing many values
A format can be repeated: '(5i4)' prints five integers, each 4 characters wide. If there are more values than descriptors, the format starts again on a new line. Arrays can be printed whole:
program many_values
implicit none
integer :: squares(8)
integer :: i
do i = 1, 8
squares(i) = i * i
end do
print '(8i4)', squares ! one line of 8
print '(4i4)', squares ! the format is reused: 2 lines of 4
print '(*(i0, 1x))', squares ! unlimited repeat, separated by one blank
end program many_values 1 4 9 16 25 36 49 64
1 4 9 16
25 36 49 64
1 4 9 16 25 36 49 64
*(...) is an unlimited repeat: it repeats the group as many times as needed on one line. 1x inserts one space.
Tip:
print '(*(g0, :, ", "))', squaresprints a comma-separated list with no trailing comma.g0means "any type, minimal width", and:stops the format when there are no more values. You'll meet these in the next lesson.
Test yourself
What does the first * in write(*, *) x mean?
- The format
- The unit: standard output
- Multiply
- All variables
Why should final results use explicit formats instead of print *?
print *is slower- List-directed spacing and digits differ between compilers
print *cannot print reals- Explicit formats use less memory
How do you keep the next output on the same line?
print *, x, advancewrite(*, '(a)', advance='no') xprint '(a)', x; continue- End the line with
&