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 another variable. The output format may be customised by a format list, similiar to printf in other programming languages. The above command to equal to write (unit=*, fmt=*) 'Hello, World!' and print *, 'Hello, World!'.

Compile the example with:

$ flang -o hello hello.f90

If you prefer GNU Fortran over Flang, run:

$ gfortran7 -o hello hello.f90

You can execute the compiled binary with:

$ ./hello
 Hello, World!

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

write (*, '(a)') 'Hello, World!'

Format

The input and output format must 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 Spaces, x times.
/ Line feed.

Format specifiers can be written in upper or lower case. Multiple format specifiers are separated by space, e. g. (a i3 a).