User Input/Output

Use the write command to output text:

! hello.f90
program main
    write (*, *) 'Hello, World!'
end program main

The two asterisks set the output unit and the format to their default values. The output unit can be either set to the screen, a file, a printer, or a variable. The output format may be customised by a format list, similiar to printf in other programming languages.

The above command is equal to write (unit=*, fmt=*) 'Hello, World!' and print *, 'Hello, World!'. Compile the example either with the GNU Fortran or the Flang compiler:

$ gfortran7 -Wl,-rpath=/usr/local/lib/gcc7/ -o hello hello.f90
$ flang -o hello hello.f90

Then, execute the compiled binary:

$ ./hello
 Hello, World!

You may notice the space before the printed text. In the old days, the first line of every output was reserved for carriage control. To remove the leading space, simply add a format specifier:

write (*, '(a)') 'Hello, World!'
Or shorter:
print '(a)', 'Hello, World!'

Format

The input and output format may be defined with format specifiers.

Format Description
Ix[.z] Integer with x digits. Optional number of leading zeros is set with z.
Bx[.z] Integer, output as binary.
Ox[.z] Integer, output as octal.
Zx[.z] Integer, output as hex.
Fx.y Fixed-point number with x digits, of which y is the number of decimal places. Sign and floating point must be regarded in x. F0.y allows a variable length.
Ex.y Floating-point number with x digits, of which y is the number of decimal places. Sign and floating point must be regarded in x.
Dx.y Floating-point number, double precision.
A String.
Ax String with x characters.
Lx Logical value.
xX Space, x times.
/ Line feed.

The Hollerith constant H is deprecated and should not be used anymore. Format specifiers can be written in upper or lower case. Multiple format specifiers are separated by comma, for example, (a, i3, a) to output 'total: ', 999, ' units'. Strings may be written inside the format specifier:

print '("total: ", i3, " units")', 999