ZetCode

Introduction to Fortran

last modified November 14, 2026

Fortran is a general-purpose, compiled programming language designed for numerical and scientific computing. It was created in the 1950s, which makes it the oldest high-level language that is still in everyday use. This article introduces the language and its standards, installs the GNU Fortran compiler, and compiles and runs the first programs.

What is Fortran

The name comes from Formula Translation. It was written in capital letters, FORTRAN, up to FORTRAN 77; since Fortran 90 the normal spelling is used, and this series follows that convention. A team at IBM led by John Backus delivered the first FORTRAN compiler for the IBM 704 in 1957. The goal was to let scientists and engineers write mathematical formulas directly, instead of assembly code, and still get machine code that was nearly as fast. That emphasis on speed for numerical work shaped the language and explains why it survives in weather and climate models, physics and chemistry codes, and numerical libraries such as BLAS and LAPACK.

Modern Fortran is a statically typed, imperative language with first-class support for arrays. Whole arrays can be added, multiplied, and passed to procedures as single values, which lets the compiler optimise them aggressively. The language also has modules, derived types, object-oriented features, pointers, and built-in parallel constructs, so a current Fortran program looks very different from the punched-card programs of the 1960s.

Fortran standards

Fortran is defined by standards, first published by ANSI and, from Fortran 90 on, by ISO. The language keeps a strong commitment to backward compatibility: a program that followed an older standard is, with few exceptions, still valid under newer ones. Each revision is informally named after the year the committee completed its draft; the publication often follows a year later, so the year in the name can differ from the year in the table.

StandardPublishedNotable additions
FORTRAN 661966the first formally standardised version
FORTRAN 771978character type, block if/else, direct-access files
Fortran 901991free-form source, modules, array syntax, allocatable arrays, derived types, pointers, select case
Fortran 951997forall, pure and elemental procedures
Fortran 20032004object-oriented programming, C interoperability, stream I/O, IEEE arithmetic
Fortran 20082010coarrays, do concurrent, submodules, block
Fortran 20182018teams and collective subroutines, further C interoperability
Fortran 20232023conditional expressions, the split and tokenize intrinsics

The break between FORTRAN 77 and Fortran 90 is the important one. Programs written before it use fixed-form source, implicit typing, and GOTO-heavy control flow. Programs written after it use free-form source, modules, and array syntax. This series teaches the modern style, with Fortran 2018 as the baseline, and treats the older style in a separate article.

Installing GNU Fortran

The examples in this series are compiled with gfortran, the Fortran front end of the GNU Compiler Collection. It is free, runs on all major platforms, and supports the standards up to Fortran 2018 almost completely. On Debian and Ubuntu it is installed with sudo apt install gfortran; on Fedora the package is gcc-gfortran, and on macOS Homebrew installs it with brew install gcc.

After the installation, gfortran --version reports the compiler release. The articles were written with version 13.2.0.

$ gfortran --version
GNU Fortran (GCC) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

The first program

A Fortran program starts with the program statement and ends with end program. The statements between them are executed in order. The program name is free to choose; it does not have to match the file name. It is repeated after end program, which is optional, but it helps when reading longer programs, and a wrong name is an error. Output goes to the screen with print.

hello.f90
program hello
    print *, 'Hello, Fortran!'
end program hello

The statement has two parts after the keyword: the asterisk * and the value to print, separated by a comma. The asterisk means "use the default format": we do not say how the output should look, and the compiler decides how to write each value. The text 'Hello, Fortran!' is a character literal, written between single or double quotes. The print statement is explained in detail in the next section.

Fortran is a compiled language, so the source file is first translated to an executable and then run. The -o option names the executable. Note the leading space in the output below. List-directed output starts every line with a blank character, a convention that comes from the carriage-control characters of old line printers. Later articles show how to remove the blank with an explicit format.

$ gfortran hello.f90 -o hello
$ ./hello
 Hello, Fortran!

The print statement

The first program used the simplest form of print. This section shows what the asterisk does and which other ways of writing output exist. The next program prints the same value with several variations to show how the asterisk and the character literals behave.

The layout of the output is how the values appear on the line: how many characters each value occupies, how it is aligned inside that space, how many decimals a real number shows, and which blanks are placed around the values. The number 42 can be written as 42, as 42 right-aligned in a field of five characters (three blanks in front of it), or as 00042 with leading zeros. Each of these is a different layout of the same value.

In list-directed output the output list alone decides the layout: the compiler looks at the type of each value (integer, real, logical, character) and writes it in a default layout for that type. The program does not describe the layout. The opposite is formatted output, where an explicit format, such as '(a, i5)', states the layout of every value. List-directed output is convenient for quick results and experiments; formatted output is used when the appearance matters.

printing.f90
program printing
    implicit none
    integer :: n

    n = 42

    print *, 'Value:', n
    print *, 'a', 'b'
    print *, 'a ', 'b'
    print *, "It's"
    print *, 'It''s'
    print *
    print '(a, i5)', 'Value:', n
    write (*, *) 'Value:', n
end program printing

The program starts with implicit none and declares the integer variable n; both are explained in the next section. The statement n = 42 stores a value in the variable. The first print writes a text and an integer. With the asterisk the compiler chooses the layout: every value is preceded by a separator blank, so the line starts with a blank, and the integer is right-aligned in a field of eleven characters. That is why the output has ten blanks before 42: the separator and nine blanks of padding. The standard does not fix these widths, so another compiler may print a different number of blanks.

Between two values the compiler inserts a blank, except when both values are character values. The statement print *, 'a', 'b' therefore prints ab; to keep the words apart, the space must be part of the literal, as in 'a '. A literal can be enclosed in single or double quotes. To put a quote character into a literal, either use the other kind of quotes ("It's") or double the quote ('It''s').

A print * without a list writes an empty line. Instead of the asterisk, the format can be given explicitly, as in print '(a, i5)', ...: a is a text field and i5 an integer in five characters. An explicit format gives full control over the layout and does not add the leading blank. The last statement, write (*, *), is the general form. The first asterisk is the standard output unit (the screen) and the second one is the list-directed format, so it prints the same as print *; the difference is that write can also send the output to a file.

$ gfortran printing.f90 -o printing
$ ./printing
 Value:          42
 ab
 a b
 It's
 It's

Value:   42
 Value:          42

Variables and arithmetic

The next program computes the area of a circle. Every variable is declared with its type, and implicit none at the top of the program tells the compiler to reject any name that was not declared. That statement belongs in every Fortran program, and the variables article explains why.

circle.f90
program circle
    implicit none
    real, parameter :: pi = 3.14159265
    real :: r, area

    r = 2.5
    area = pi * r**2

    print *, 'Radius:', r
    print *, 'Area:', area
    write (*, '(a, f8.3)') 'Area:', area
end program circle

The parameter attribute makes pi a named constant. The assignment r = 2.5 stores a value in the variable, and the exponent operator is **, so r**2 squares the radius. The program prints the area twice. The first print uses list-directed output, which shows a real number with enough digits (nine significant digits for single precision) to tell it apart from the neighbouring single-precision numbers. The second statement uses write with the format (a, f8.3): a text field followed by a real number in a field of eight characters with three decimals.

$ gfortran circle.f90 -o circle
$ ./circle
 Radius:   2.50000000
 Area:   19.6349545
Area:  19.635

Compiler options

Compiler options change what the compiler does: some name the output file, some add checks for suspicious code, and some change how the code is generated. The ones used in this series are listed below. The programs of every article were compiled with -std=f2018 -Wall -Wextra -fcheck=all and produced no warnings, unless the article is about a warning. To keep the transcripts short, the commands shown in this article leave these options out, except where an option is the topic.

OptionMeaning
-o namename of the executable
-std=f2018reject constructs that are not part of the Fortran 2018 standard
-Wallenable the common warnings, such as unused variables
-Wextraenable additional warnings
-fcheck=alladd run-time checks, such as array bounds
-gadd debugging information
-O2optimise the generated code

The next program declares a variable and never uses it. Without -Wall the compiler stays silent; with the option it reports the unused variable and shows the position of the declaration.

unused.f90
program unused
    implicit none
    integer :: used, unused_var

    used = 5
    print *, used
end program unused

The warning is printed during compilation and the program still runs, because a warning is not an error. The number 5 in the output is padded to the default width of an integer in list-directed output.

$ gfortran -Wall unused.f90 -o unused
unused.f90:3:31:

    3 |     integer :: used, unused_var
      |                               1
Warning: Unused variable ‘unused_var’ declared at (1) [-Wunused-variable]
$ ./unused
           5

Source files and extensions

The extension of a source file tells gfortran how to read it. The programs in this series use free-form source and the extension .f90, which works for every standard from Fortran 90 on, not only for Fortran 90 itself.

ExtensionSource form
.f90, .f95, .f03, .f08free form
.F90, .F95, ...free form, run through the C preprocessor first
.f, .forfixed form (the FORTRAN 77 layout)

A common mistake is to put free-form code into a file with the extension .f. The compiler then reads it as fixed form, where statements must start in column 7, and reports confusing errors. The program structure article returns to this point.

Next steps

The program structure article describes the parts of a program and the rules of free-form source. The variables article covers declarations and constants, data types and kinds describes the intrinsic types and their precision, and literals and constants shows how values are written in the source text. From there the series continues with operators and expressions, conditionals, and loops.

Source

The GNU Fortran Compiler

WG5: the ISO Fortran standards committee

Fortran-lang: the Fortran community site

Fortran on Wikipedia

In this article we have introduced Fortran, looked at its standards from FORTRAN 66 to Fortran 2023, installed GNU Fortran, and compiled the first programs. The series continues with Fortran program structure.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Fortran tutorials.