Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Fortran output have a leading space?

I process lots of output from Fortran programs, and it generally appears that the output from these programs has a leading space, i.e., character column 1 is blank. Consider a short program leading.f:

      program leading
      print *, "<-- What's that space for?"
      end program leading

Has the output

 <-- What's that space for?

(yes, there is one space character at the beginning). My question is contained in the program.

like image 437
Mike T Avatar asked May 16 '12 22:05

Mike T


2 Answers

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!

like image 185
Jim Lewis Avatar answered Oct 26 '22 05:10

Jim Lewis


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.)

like image 35
francescalus Avatar answered Oct 26 '22 05:10

francescalus