Why does Fortran output have a leading space?

Back in the dinosaur era, when FORTRAN output usually went to a green-bar impact printer, certain characters in the first print column were often interpreted as control codes (line feeds, form feeds, etc). Many programmers learned to explicitly blank column 1 of their output, unless some special effect was intended -- and old habits die hard!


As has been noted in another answer here, and elsewhere, Fortran output had the concept of carriage control. For printers which used carriage control the first character being a blank was necessary for a new line to be started.

Fortran itself deleted carriage control concept in Fortran 2003, but for completeness we can see that Fortran still requires list-directed output to have (in most cases) this (default) leading blank (Fortran 2018, 13.10.4 p.13):

Except for new records created by explicit formatting within a defined output procedure or by continuation of delimited character sequences, each output record begins with a blank character.

Namelist formatting has a similar statement.

You can avoid having this leading blank by avoiding using list-directed output:

print '(A)', '<-- No space here'
end

Note that it isn't the print here, but the list-directed output, which is to blame. We see similar with write:

write (*,*) '<-- Space from the list-directed output'
end

Finally, if we are using internal files we still get a leading blank with list-directed output:

character(len=20) :: internal
write (internal, *) '<-- Leading blank here'
end

(If we then output this internal file with list-directed output we'll see two leading blanks.)