Reading and writing fixed form
Every Fortran file written before 1990, and a large part of what is still maintained, is in fixed form: the statement does not begin in the first column, the first five columns are reserved for a label, the sixth is the continuation mark, and everything after column 72 is silently dropped. A compiler still reads it, a reader of the nineteen-eighties wrote it, and a modern maintainer has to know all three facts. This lesson builds an old program, shows what the same file means to a free-form compiler, and demonstrates the truncation that changes an answer without a message.
Columns 1 to 72
A fixed-form line has four fields. Columns 1 to 5 hold a statement label, or blanks; column 6 holds a continuation character, or a blank or a zero; columns 7 to 72 hold the statement; and anything after column 72 is ignored. Column 1 is the special one: a C, c or * there makes the whole line a comment, which is how old code carries its headings.
The pages of this course show fixed form in blocks whose first line is such a comment, and the checker builds those blocks as fixed form with the command this page shows. A free-form file cannot start a line with C and a space, because C would be a name and the space would end the statement, so one column-1 comment is enough to tell the two forms apart.
The ruler below has the column numbers on its first line, so that the four fields can be counted:
1234567890123456789012345678901234567890 C SUM OF SQUARES, IN FIXED FORM PROGRAM SQUARES 10 CONTINUE TOTAL = TOTAL + I*I & I5)
The comment starts in column 1, PROGRAM in column 7, the label 10 in columns 4 and 5, and the & in column 6 marks the continuation line. A fortrancode block keeps its spaces, so the program below can be counted against the same ruler.
An old program
The program is the sum of the squares of one to five, written the way a textbook of 1978 would: a labelled DO, a labelled FORMAT and upper case. The continuation matters for the format, which is split after the comma.
The file old.f:
C SUM OF SQUARES, IN FIXED FORM
PROGRAM SQUARES
IMPLICIT NONE
INTEGER I, TOTAL
TOTAL = 0
DO 10 I = 1, 5
TOTAL = TOTAL + I*I
10 CONTINUE
PRINT 20, TOTAL
20 FORMAT('TOTAL = ',
& I5)
END
The six blanks in front of PROGRAM are not indentation: they put the statement in column 7. The label 10 sits in columns 4 and 5, and the & in column 6 of the last format line continues the line above it. The build needs two flags beyond the ones the rest of the course uses:
gfortran -Wall -Wextra -std=legacy -ffixed-form old.f -o old
./old
TOTAL = 55
-ffixed-form is the form, and -std=legacy is what accepts the labelled DO without an error, because the labelled form is obsolescent in Fortran 2018 and gfortran refuses some of these features under -std=f2018. A file named .f, .for or .ftn is fixed form without the flag, which is why old code keeps its extension.
The same file as free form
Copy the file to old.f90 and the compiler reads it as free form, because the extension says so. Column one is then the first column of a statement, and the comment is no longer a comment:
cp old.f old.f90
gfortran -Wall -Wextra -std=f2018 old.f90 -o old90
old.f90:1:1:
1 | C SUM OF SQUARES, IN FIXED FORM
| 1
Error: Unclassifiable statement at (1)
old.f90:6:20:
6 | DO 10 I = 1, 5
| 1
Warning: Fortran 2018 obsolescent feature: Labeled DO statement at (1)
The first message is the one that matters: Unclassifiable statement at line 1, column 1. A reader who sees that message on an old file knows the file is fixed form and the extension is wrong, and the fix is mv old.f90 old.f rather than an edit of every line. The second message is the same obsolescence the build above silenced with -std=legacy.
The continuation column
A statement that does not fit continues on the next line when column 6 of that line is neither blank nor zero, and the statement text starts again in column 7. In the program above the format is 'TOTAL = ', I5, split between the two lines, and the compiler sees one statement. Old code uses &, +, 1 or any other character there, which is why a column-6 digit can be mistaken for part of a label by a reader who counts from one.
Two lines that both look like continuations are a classic shape:
IF (N .GT. 0) THEN X = A + B & + C END IF
The second line assigns a + b + c; the same two lines in free form would be an assignment and a syntax error, because free form continues only inside a statement that is still open, with a & at the end of the previous line. That snippet is a listing, not a program: no page builds it.
The 72-column trap
The dangerous field is the last one. A statement that runs past column 72 loses its tail without an error, and the program compiles and gives a different answer. This program adds eight thousands to a total of one, on a line of 75 characters:
The file long.f:
C ONE LINE TOO LONG
PROGRAM LONG
IMPLICIT NONE
INTEGER TOTAL
TOTAL = 1
TOTAL = TOTAL + 1000 + 1000 + 1000 + 1000 + 1000 + 1000 + 1000 + 1000
PRINT 20, TOTAL
20 FORMAT('TOTAL = ', I5)
END
gfortran -Wall -Wextra -std=legacy -ffixed-form long.f -o long
./long
long.f:6:72:
6 | TOTAL = TOTAL + 1000 + 1000 + 1000 + 1000 + 1000 + 1000 + 1000 + 1000
| 1
Warning: Line truncated at (1) [-Wline-truncation]
TOTAL = 7002
The source adds eight thousands to one, which is 8001. Column 72 falls inside the last group of digits, the compiler keeps + 1 of it, and the program prints 7002: seven thousands, the one it started with and one more from the cut. -Wall prints the warning, and a build that treats warnings as errors turns this one into a failure; a build without -Wall says nothing at all.
Whenever an old program prints a number that is off by a little, count the columns of the line that computes it. The fix is a continuation line, and the mechanical one is to run the file through a converter that folds long statements. Neither changes the arithmetic, which is the point.
Which files are fixed form
The extension decides by default: .f, .for, .ftn and .f77 are fixed form, and .f90, .f95, .f03 and .f08 are free form. The flags -ffixed-form and -ffree-form override the extension, which is how a project builds a file whose name lies. gfortran can also read a fixed-form source named .f90 when -ffixed-form is on the command line, and that is the combination this course uses for its fixed-form pages.
A project that still has fixed-form files compiles them with their own rule in the Makefile, one line per extension, and does not rename them: a rename to .f90 changes the meaning of every line, as the section above shows.
Reading old code
Read the comments in column 1 first, then count the columns of the first executable line to find where statements begin, then look at column 6 of every line that continues one. Upper case is not a statement about the language: Fortran has been case insensitive since FORTRAN 77, and TOTAL and total are the same variable. A name longer than six characters, a DO with a label, a label in columns 1 to 5 and a format statement are the four marks of the era, and the next two lessons take them one at a time: the storage statements that share memory, and the control flow that jumps.
Exercises
- Move the I5 of the format back onto one line. How long is the line, and does the build still need the continuation?
- Change the label 10 to 100000. What does the compiler say, and why?
- Put a * in column 1 of the first line instead of C. Does the program still build?
- Build old.f with -ffree-form added after -ffixed-form. Which one wins, and what does the error look like?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Where does a fixed-form statement begin?
Answer
Column 7
Columns 1 to 5 are for a label.
-
What does a C in column 1 mean?
Answer
The line is a comment
A c or a * works the same way.
-
What goes in column 6?
Answer
The continuation mark
Any character except blank or zero continues the line.
-
What happens to characters after column 72?
Answer
They are dropped, with a warning under -Wall
The program above printed 7002 instead of 8001.
-
Which extension means fixed form?
Answer
.f
The Fortran 90 extensions are free form.
-
What does -std=legacy add to the build?
Answer
It accepts obsolescent features such as the labelled DO
-ffixed-form is the other flag.
-
What does a free-form compiler say about a column-1 comment?
Answer
Unclassifiable statement at (1)
The first message of the .f90 build.
-
Why does uppercase not matter?
Answer
Fortran has been case insensitive since FORTRAN 77
TOTAL and total are the same variable.
-
How does an old program usually carry a long statement?
Answer
On a continuation line with a mark in column 6
The format of the first program is split that way.
-
What is the first thing to check when an old program prints a slightly wrong number?
Answer
Whether the statement runs past column 72
Truncation changes the expression without an error.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev