044. List-directed vs. explicitly formatted output#

topic: Input and Output

List-directed output (format *) is convenient,

! Examples
print *, x, y, z
write (*, *) x, y, z
write (unit_integer, *) x, y, z

but don’t use it when you care about layout as you’ll lose control over spacing, line breaks and number of significant digits. If you care, use explicit formats instead.

list-directed.f90 | | Godbolt Compiler Explorer logo | Fortran logo#
program list_directed
   use iso_fortran_env, only: real64
   implicit none

   integer, parameter :: x = 1
   real(kind=real64), parameter :: y = acos(-1.0), &
      z = 6.62607015d-34

   print *, x, y, z
   print '(*(g0, :, x))', x, y, z
   print '(*(g0.4, :, ","))', x, y, z
   print '(/, i0, /, "and", /, f0.5, /, "and", /, es0.3)', x, y, z

end program list_directed
Output1#
           1   3.1415927410125732        6.6260701499999998E-034
1 3.1415927410125732 0.66260701499999998E-33
1,3.142,0.6626E-33

1
and
3.14159
and
6.626E-34

See Tip 041 for g0.d and : meaning in format syntax.



1

Compiled using GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 with no flags