A Fortran source file is a text file of lines. The language standard says nothing about file names, extensions, line endings or encodings: those belong to the compiler. What the standard does define is the source form of the lines (fixed or free) and the program units a file may hold.
File extensions
The extension is how most compilers choose the source form, and whether to run the
preprocessor. The table lists the extensions in common use. A capital letter in the
extension (.F, .F90) is a convention of Unix compilers for
“run the C preprocessor first”; the viewer treats upper and lower case alike.
| Extension | Source form | Usual meaning |
|---|---|---|
.f90 | Free | Fortran 90 and later. The extension did not change with later standards, so it names the form, not the language level |
.f95, .f03, .f08 | Free | Free form written for Fortran 95, 2003 and 2008; compilers treat them like .f90 |
.f18, .f23 | Free | Used for Fortran 2018 and 2023 by some projects; compilers treat them like .f90 when told they are Fortran |
.f, .for, .ftn, .f77 | Fixed | The punched-card layout of FORTRAN 66 and 77. .for is the usual name on VMS and DOS; .f77 is used by some Unix tools |
.F, .FOR, .FTN, .fpp, .FPP | Fixed | Fixed form, run through the preprocessor first (GNU Fortran lists exactly these) |
.F90, .F95, .F03, .F08 | Free | Free form, run through the preprocessor first |
.cuf, .CUF | Free | CUDA Fortran (NVIDIA nvfortran): free form with GPU attributes and kernel launches |
.pf | Free | pFUnit test files, which a preprocessor turns into Fortran |
.inc, .h | Either | Include files, read by an INCLUDE line; their form is that of the file that includes them |
.mod, .smod | (binary) | Not source: module and submodule interface files that the compiler writes. Their layout is the compiler’s own, and differs between compilers and versions |
.f is also used for Forth source and, rarely, for other languages. The viewer
reads a .f file as Forth, and shows it without colour, when it opens with
colon definitions and \ comments and holds no Fortran statements.
GNU Fortran, Intel, Flang and the others let a flag override the extension:
-ffree-form and -ffixed-form in GNU Fortran and Flang,
-free and -fixed in Intel Fortran, and -qfree and
-qfixed in IBM XL Fortran.
Program units
A file holds one or more program units, written one after another. Each unit
ends with its own end statement. The kinds are:
- main program:
program name … end program name. Theprogramline is optional in every version, so a file of plain statements ending inendis a main program - external subprogram: a
subroutineorfunctionoutside any module - module:
module name … contains … end module, holding declarations and module procedures (Fortran 90) - submodule:
submodule (parent) name … end submodule, holding the bodies of a module’s separate module procedures (Fortran 2008) - block data:
block data name … end block data, giving initial values to common blocks (FORTRAN 66 and later; obsolescent from Fortran 2018)
Inside a program unit, contains introduces internal or module
subprograms. Interface blocks and derived-type definitions are specifications, not program
units. See Program units and procedures.
module shapes
implicit none
real, parameter :: pi = 3.14159265
contains
pure function area(r) result(a)
real, intent(in) :: r
real :: a
a = pi * r**2
end function area
end module shapes
program demo
use shapes
implicit none
print *, area(2.0)
end program demo
A compiler needs a module’s source compiled before any unit that uses it,
so build tools order the files. Common practice is one module per file, named after the module. The
Fortran Package Manager (fpm) expects src/, app/ and
test/ folders and a fpm.toml manifest.
Include lines
include 'file' is a line, not a statement of the language proper: the compiler
replaces it by the text of the named file before reading on. It is part of the standard since Fortran 90
(FORTRAN 77 compilers all had it as an extension). An include line must be alone on its line, and
a file may include others up to a compiler-set depth. Separately, the C preprocessor’s
#include works in files that are preprocessed (see
Directives and preprocessor).
Character set, case and names
- The Fortran character set is the letters, the digits, the underscore, and the special characters
blank
= + - * / ( ) [ ] , . : ; ' " ! % & < > ? $. Comments, character literals and format texts may hold any character the processor has. - Upper and lower case are the same everywhere except inside character literals (and
Hand'…'edit descriptors).PROGRAM,Programandprogramare one word. - The viewer reads the bytes as UTF-8 and falls back to Windows-1252, so accented letters in comments and strings show correctly. Old files in other 8-bit sets show the Latin-1 reading.
- Fortran 2003 added the ISO 10646 (Unicode) character kind, selected by
selected_char_kind('ISO_10646'); compilers read UTF-8 source for it. - A tab is not in the standard character set. Every compiler accepts it as a blank in free
form, and the DEC compilers give it a meaning in fixed form (tab format).
A form feed (Ctrl-L) is blank space to GNU Fortran. A Ctrl-Z (
0x1A) at the end of a DOS file is an end-of-file mark.
Line endings, length and the end of the file
Lines end with LF, CR LF or (on classic Mac OS) CR; compilers read them all, and the viewer shows CR LF files with no stray characters. The standard counts characters per line, not bytes:
| Source form | Standard line length | Compiler options for longer lines |
|---|---|---|
| Fixed (FORTRAN 66, 77, Fortran 90 and later) | 72 characters; columns 73 and after are ignored | GNU Fortran -ffixed-line-length-n (default 72; none allowed), Intel -extend-source, IBM -qfixed=n |
| Free, Fortran 90 to 2018 | 132 characters | GNU Fortran -ffree-line-length-n (default 132; none allowed) |
| Free, Fortran 2023 | 10 000 characters (the standard raised the limit) | — |
The last line of a file need not end in a line ending. A file may be empty: a compiler then makes no
program unit. Comment lines, blank lines and lines of only ; are allowed between program units.
Preprocessing
The Fortran standard has no preprocessor. Real projects use one of three:
cppin traditional mode, or the compiler’s ownfpp, which understand#if,#defineand#includelines. GNU Fortran runs it for the extensions in the table above and for-cpp; it is switched off by-nocpp- Coco, the conditional-compilation part of the ISO standard 1539-3, with
??lines (?? if (…) then). It was never widely used, and is not a part of the language - Tools that generate Fortran from templates:
fypp,pFUnit’s.pffiles, and macro systems such asm4
Preprocessor lines are shown as directives. See Directives and preprocessor.
Choosing the source form: how the viewer guesses
A file may be in either form whatever its extension says: .f files in free form are common in
newer projects, and fixed-form code with :: and ! comments is common in the middle (the
Fortran 90 form of old programs). Compilers trust the extension or a flag. The viewer
also reads the text:
- Fixed-form signs: a
C,cor*followed by a blank in column 1; a statement label in columns 1 to 5 followed by a blank in column 6; a continuation mark in column 6 with columns 1 to 5 blank; a tab at the start of a line; comments such ascf2pyorCDEC$in column 1 - Free-form signs: a statement that starts before column 7 (
programorendin column 1, an indentedimplicit none), a line ending in&, a line starting with&,::and a!after code on such lines
For a free-form extension (.f90 and its relatives) the text is taken as fixed form only when its
fixed-form signs clearly outweigh the free-form ones. For .f, .for, .ftn, .f77 and
.fpp the text is taken as free form only when the free-form signs clearly win. Text that is neither
(Forth, or no Fortran at all) is shown without colour. The overview tab names the form that was chosen.
See also
- Fixed source form and free source form for the line rules
- Versions and standards for what each version of the language added