Source Format

For decades, Fortran code was written in fixed source format, with reserved areas for statement labels and indentification fields, caused by the fixed form of Hollerith punch cards. That means, the first six characters of each line are reserved for line numbers, comment identifiers, continuation characters, and so on. Lines were usually limited to 72 characters.

With Fortran 90, free source format was introduced, which abolished indention patterns and improved readability (tab. 1). Since Fortran 95, fixed source format is declared obsolescent.

! f2c.f
! Example in fixed source format.
      PROGRAM F2C
      IMPLICIT NONE
      REAL :: C, F

      PRINT *, '  Fahrenheit     Celsius'
      PRINT *, '--------------------------'

! Output table:
      DO F = 30, 220, 10
      C = (5.0 / 9.0) * (F - 32.0)
      PRINT '(F13.1, F12.3)', F, C
      END DO
      END PROGRAM F2C
! f2c.f90
! Example in free source format.
program f2c
    implicit none
    real :: c, f

    print *, '  Fahrenheit     Celsius'
    print *, '--------------------------'

    ! Output table:
    do f = 30, 220, 10
        c = (5.0 / 9.0) * (f - 32.0)
        print '(f13.1, f12.3)', f, c
    end do
end program f2c
Table 1: Fortran source code in old upper-case fixed format (left), and modern free format (right)

Modern Fortran compilers provide command-line flags in order to distinguish between the old fixed format and the modern free format. Use -ffree-form to force free format with GNU Fortran:

$ gfortran13 -ffree-form -o f2c f2c.f

Same with Flang:

$ flang -ffree-form -o f2c f2c.f

File Endings

Historically, the file endings of Fortran source files are .f and .for. These are associated with Fortran < 90, and therefore with fixed format. Even modern compilers assume fixed format for them, unless the free format command-line flag is used.

For this reason, file endings should be used that represent modern Fortran versions. We can either set endings that correspond with the actual language version (.f03 for 2003, .f08 for 2008, .f18 for 2018, and so on) or simply use .f90 as a default to indicate modern Fortran, with the latter being recommended. Be aware that you must set the file ending to either .f90 or .f95 to be recognised by CMake.