Free form and fixed form

Fortran has two source forms, and the difference is not cosmetic: the same characters mean different things depending on which form the compiler is reading. One is the modern form, where the line is the line and a comment starts with an exclamation mark. The other is the form of the 1966 standard, where the first six columns of a card had fixed jobs. This lesson shows both, builds the same program in both, and ends with the error a reader gets when a file is read in the wrong form.

Two source forms

The extension of the file selects the form, not the content: .f90 is read as free form, .f, .for and .f77 as fixed form, and the flags -ffree-form and -ffixed-form override the extension when a file is misnamed.

Free form

The program below uses the two free-form conveniences. The comment starts with ! and runs to the end of the line; the assignment continues after a trailing &, which lets a long expression be broken without a line-length limit. A leading & on the continuation line is optional in free form and required only when the split falls inside a character constant.

The file freeform.f90:

program freeform implicit none integer :: total ! A comment runs to the end of the line. total = 10 + & 20 print *, "total:", total end program freeform

gfortran -Wall -Wextra -std=f2018 freeform.f90 -o freeform
./freeform

total: 30

Free form has no column rules at all, so indentation is entirely a matter of style. It also has no label column, which is why a modern program puts labels at the start of the statement, as in 10 continue, and why the GOTO forms that needed the column are rare in code written after 1990.

Fixed form

The same three facts, written the way a card deck wrote them. The first line starts with C in column 1 and is a comment. Each statement starts in column 7, which is what the six spaces at the beginning of every line are for. The continuation line has a & in column 6 and the rest of the statement from column 7.

The file fixed.f:

C A fixed-form program: the C sits in column 1. PROGRAM FIXED INTEGER TOTAL TOTAL = 10 + & 20 PRINT *, "total:", TOTAL END

A fixed-form compiler reads it with the historical rules, and the command has to say so twice: the extension .f already selects fixed form, and -ffixed-form makes the choice explicit. -std=legacy is what accepts the form at all, because -std=f2018 treats fixed form as an extension.

gfortran -Wall -Wextra -std=legacy -ffixed-form fixed.f -o fixed
./fixed

total: 30

The two programs print the same line, which is the point: the source form decides how the characters are read, not what the program does. Everything the language can express can be written in either form, and the fixed form of a modern program is just a modern program with six spaces in front of every line.

The same program in both forms

Side by side, the differences are small and mechanical:

A fixed-form file read as free form

The common accident is a file saved as .f90 that still starts its comment lines with C. Free form reads the C as a statement, and there is no statement that begins with a lone C followed by text.

The file fixed_as_free.f90 still holds a fixed-form file. Here is what it contains, and what a file extension of .f90 does to it:

cat fixed_as_free.f90

C This line is a comment in fixed form only.
      PROGRAM MIXED
      PRINT *, "hello"
      END

gfortran -Wall -Wextra -std=f2018 fixed_as_free.f90 -o mixed

fixed_as_free.f90:1:1:

1 | C This line is a comment in fixed form only.
| 1
Error: Unclassifiable statement at (1)

The file is not built on this page, because the compiler refuses it: the listing above is the exact content and the exact diagnostic, and there is no binary to run.

The fix is one character: replace the C with !. The other fix, renaming the file to .f and building with -ffixed-form, keeps the file as the legacy code it is, and that is the right choice when the file came from an old project.

Why fixed form still matters

Fixed form is not history. The numerical libraries that new code links against were written in it, and a reader who opens dgemm.f from reference BLAS meets columns, labels and CONTINUE statements on the first screen. Reading it is a skill, and the lesson on legacy fixed form teaches the rest of it: COMMON, EQUIVALENCE, arithmetic IF and the labelled DO loop.

Two rules keep a modern reader out of trouble. Write new code in free form with a .f90 extension, and never mix the two forms inside one file. When an old file must be read, ask the compiler which form it is in before changing anything: a C in column 1 is the answer, and the build command with -ffixed-form is the proof.

The code on this page follows that rule too: a block whose first line is a C comment in column 1 is a fixed-form program, and it was built with the legacy command shown above.

Exercises

  1. Rewrite the fixed-form program in free form and build it with -std=f2018. Which line changes most?
  2. Move the C comment in the free-form program from column 1 to column 7. Does it build now?
  3. Put two statements on one line in free form, separated by a semicolon. Does fixed form allow the same?
  4. Build the fixed-form program with -std=f2018 -ffixed-form. What does the compiler say, and why?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which extension makes gfortran read a file as fixed form?

    Answer

    .f

    .f, .for and .f77 are fixed form; .f90 and later numbers are free form.

  2. What makes a line a comment in fixed form?

    Answer

    A C, c or * in column 1

    The column-1 letter is the original card comment marker.

  3. What is column 6 of a fixed-form source line for?

    Answer

    It marks a continuation line

    Any character other than a blank or a zero in column 6 continues the previous statement.

  4. How does a free-form statement continue on the next line?

    Answer

    A trailing & ends the line and the statement continues

    The ampersand is the free-form continuation marker.

  5. What does a column-1 C line give in a .f90 file?

    Answer

    Error: Unclassifiable statement at (1)

    Free form reads C as the start of a statement, and no statement starts that way.

  6. Which flag forces fixed form on a file named .f90?

    Answer

    -ffixed-form

    -ffixed-form overrides the extension; -std=legacy alone does not change the form.

  7. What does this program print? program p implicit none integer :: total total = 10 + & 20 print *, total end program p

    Answer

    30

    The trailing ampersand continues the expression, so total is 30.

  8. Which columns of a fixed-form line hold the statement?

    Answer

    7 to 72

    Columns 1 to 6 are label and continuation, and 73 to 80 were outside the card.

  9. Why does the fixed-form example need -std=legacy?

    Answer

    Fortran 2018 treats fixed form as an extension, so the strict standard rejects it

    The modern standard no longer includes the fixed source form, so the strict check refuses it.

  10. Can one file mix free-form and fixed-form lines?

    Answer

    No: the compiler reads the whole file in one form

    The source form is a property of the file, chosen by extension or flag.

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. The fixed-form programs were built with -std=legacy -ffixed-form.

Article author: Arthur Isaev