Introduction
Fortran is the compiled language developed at IBM by a team led by John Backus and first released in 1957. It remains in use for scientific and numerical computing. The language is an ISO standard, revised as Fortran 90, 95, 2003, 2008, 2018 and 2023.
The extension tells the compiler how the source is laid out, not which version of the
language it uses. .f, .for, .f77,
.ftn and .fpp mean fixed form, the punched-card layout of early Fortran.
.f90, .f95, .f03, .f08, .f18,
.f23 and .cuf mean free
form. On Linux and macOS a capital letter, as in .F or .F90,
usually asks the compiler to run the C preprocessor first.
.f is also used for Forth source by some Forth systems; the viewer shows such a file without colour.
This one page and its reference pages cover both source forms and every version of the language, FORTRAN 66 to Fortran 2023.
The viewer opens every extension above, reads fixed or free form from the text when the extension is not enough, and
names the form it chose.
The reference
- File structure – extensions, program units, include lines, character set, line endings, preprocessing and how the source form is guessed
- Fixed source form – columns, labels, continuation, comments, sequence numbers, tab format, insignificant blanks
- Free source form –
!comments,&continuation, separators, labels, line limits, optional blanks - Lexical structure – names, keywords that are not reserved, every literal form, operators and delimiters
- Types and declarations – intrinsic types and kinds, attributes, implicit typing, arrays, pointers,
common,data - Expressions and arrays – precedence, array sections and constructors,
where, defined operators, conditional expressions - Statements and control flow – every statement and construct, and all the keywords
- Program units and procedures – programs, subprograms, modules, submodules, interfaces, arguments, C interoperability
- Derived types and objects – components, extension, type-bound procedures, polymorphism, finalisation
- Input and output – units,
open,read,write,inquire, file kinds, namelists - Format edit descriptors – every descriptor, repeat counts and reversion
- Intrinsic procedures – every intrinsic, by group and standard
- Intrinsic modules –
iso_fortran_env,iso_c_binding, the IEEE modules - Parallel features – coarrays, teams, events,
do concurrent, OpenMP, OpenACC, CUDA Fortran - Directives and preprocessor – sentinel comments,
cpp, coco,include - Versions and standards – FORTRAN 66 to Fortran 2023, deleted and obsolescent features
- Extensions and dialects – VAX and DEC, Cray, Hollerith, GNU extensions
Structure
A Fortran file is plain text, and the language ignores case. A file holds program
units: one program and any number of module,
subroutine and function units, each closed by
end.
In fixed form, position on the line has meaning:
- column 1:
C,cor*makes the line a comment - columns 1 to 5: an optional statement label, a number
- column 6: any character except a space or zero continues the previous line
- columns 7 to 72: the statement
- columns 73 and beyond: ignored, once used for card sequence numbers
PROGRAM HELLO
C Statements start in column 7.
INTEGER I
DO 10 I = 1, 3
WRITE (*,*) 'HELLO', I
10 CONTINUE
END
In free form, statements can start anywhere on a line of up to 132
characters (10,000 in Fortran 2023). Comments start with !, and a line ending in
& continues on the next.
program hello
implicit none
integer :: i
do i = 1, 3
print *, 'Hello', i ! one line for each pass
end do
end program hello
Not described
Known, not yet covered
- Reference: the J3 working drafts of Fortran 2003, 2008, 2018 and 2023 and the NIST scan of the FORTRAN 77 standard were downloaded but are compressed PDFs that this site’s tools could not turn into text. The pages are checked against the plain-text FORTRAN 77 standard, the GNU Fortran manual (extensions, dialect options, intrinsic list, directives list) and the standards’ public summaries, and the rest is from knowledge of the standards. Section numbers and exact limits quoted from Fortran 2003 to 2023 have not been checked against the drafts. See Versions and standards.
- Reference: the exact membership of the deleted and obsolescent feature lists of Fortran 2018 and 2023 (Annex B of the standard), taken here from Wikipedia’s summary. See Versions and standards.
- Reference: the Fortran 90 and 95 drafts were not found online, so those versions are described from the later standards, the FORTRAN 77 text and the GNU Fortran manual.
- Reference: the OpenMP and OpenACC directive and clause lists are written from knowledge of OpenMP 5.2 and OpenACC 3.3 (the specifications were downloaded but not text-extracted); the clauses of each directive are not described one by one. See Directives and preprocessor.
- Reference: the Intel Fortran and IBM XL Fortran language references were not read, so vendor options and directives are described from the GNU Fortran manual and general knowledge. See Extensions and dialects.
- Reference: the layout of compiler module files (
.mod,.smod) and of unformatted record files is compiler-specific and only named here. - Viewer: a character literal used as a format (
write (*, '(a, i5)')) is one string, so the descriptors inside it are not coloured; onlyformatstatements have coloured descriptors. - Viewer: code that a preprocessor conditional switches off is coloured like any other code, and macros are not expanded.
- Viewer: a user-defined function or subroutine is coloured as a function only when its definition is in the same file (or it follows
call); a name from another file looks like an array. The words of an OpenMP or OpenACC directive are one directive colour, with no separate colours for clauses. - Viewer: fixed-form names that contain blanks (
MY VAR) and keywords split by blanks in places other than the start of a statement (IF (X) TH EN) are not recognised.
Unknown
- Viewer: the colouring was checked on about 1 500 Fortran files on this computer (mostly free-form test files, LAPACK-style fixed form and a few legacy programs) and on hand-written cases, never against a compiler. Rules that depend on a compiler’s dialect flags (
-fd-lines-as-code,-ffixed-line-length-none,-fdec) follow the most common reading.
Links
Software
- GNU Fortran –
gfortran, the free compiler in GCC (Windows, macOS, Linux) - Intel Fortran Compiler – Intel’s
ifxcompiler (Windows, Linux) - Flang – the Fortran compiler in the LLVM project (Linux, macOS, Windows)
- fpm – the Fortran Package Manager (Windows, macOS, Linux)
Developers
- fortls – language server for Fortran (Python)
- tree-sitter-fortran – Fortran grammar for the Tree-sitter parser used by editors
Reference
- Fortran-lang – community site with tutorials and documentation
- WG5 – the ISO working group that maintains the standard
- GNU Fortran manual – how
gfortrantreats each extension and source form - Fortran 2023 working draft (J3 23-007r1) – the free draft text of ISO/IEC 1539-1:2023, with older drafts for 2018, 2008 and 2003
- OpenMP specifications – the directives written as
!$ompcomments - OpenACC specification – the directives written as
!$acccomments - ISO/IEC 1539-1 – the international standard for Fortran