Intermediate

Hello World in Fortran

Your first Fortran program - the classic Hello World example with Docker setup

Every programming journey starts with Hello World. Let’s write our first Fortran program using modern free-form syntax.

The Code

Create a file named hello.f90:

1
2
3
4
program hello
    implicit none
    print *, "Hello, World!"
end program hello

Understanding the Code

  • program hello - Declares the start of a program named “hello”
  • implicit none - Disables implicit variable typing (best practice in modern Fortran)
  • print *, "Hello, World!" - Outputs text to the console. The * means use default formatting.
  • end program hello - Marks the end of the program block

Why implicit none?

Classic Fortran had implicit typing: variables starting with I-N were integers, others were real numbers. This caused countless bugs when typos created unintended variables. Modern Fortran best practice is to always use implicit none to require explicit variable declarations.

Running with Docker

The easiest way to run Fortran without installing a compiler locally:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Create the source file
cat > hello.f90 << 'EOF'
program hello
    implicit none
    print *, "Hello, World!"
end program hello
EOF

# Compile and run with GCC's gfortran
docker run --rm -v $(pwd):/app -w /app gcc:latest \
    sh -c "gfortran -o hello hello.f90 && ./hello"

Running Locally

If you have gfortran installed:

1
2
3
4
5
# Compile
gfortran -o hello hello.f90

# Run
./hello

Installing gfortran

macOS (Homebrew):

1
brew install gcc

Ubuntu/Debian:

1
sudo apt install gfortran

Windows: Download MinGW-w64 or use WSL.

Expected Output

 Hello, World!

Note the leading space - Fortran’s default output includes a “carriage control” character position, a holdover from line printer days.

Alternative Syntax

You’ll see variations in Fortran code:

Using WRITE instead of PRINT

1
2
3
4
program hello
    implicit none
    write(*, '(A)') "Hello, World!"
end program hello

The '(A)' format specifier outputs the string without the leading space.

Classic FORTRAN 77 Style (Fixed-Form)

1
2
3
      PROGRAM HELLO
      WRITE(*,*) 'Hello, World!'
      END

This fixed-form style requires specific column positions. Modern code should use free-form.

Key Concepts

  1. Fortran is compiled - Source code (.f90) is compiled to a native executable
  2. Program structure - Every program needs program name and end program name
  3. Case insensitive - PRINT, Print, and print are identical
  4. implicit none is essential - Always include it to catch typos
  5. File extensions matter - Use .f90 for free-form, .f or .f77 for fixed-form

Compiler Options

Useful gfortran flags for development:

1
2
3
4
5
6
7
8
# Enable all warnings and debugging
gfortran -Wall -Wextra -g -o hello hello.f90

# Maximum optimization for production
gfortran -O3 -o hello hello.f90

# Check array bounds at runtime (catches many bugs)
gfortran -fcheck=bounds -o hello hello.f90

A Bit of History

When FORTRAN was released in 1957, this simple program would have been punched onto cards:

Column:  123456789...
         CPROGRAMHELLO
         C     WRITE(6,100)
         C100  FORMAT('HELLO, WORLD!')
         C     END

The fact that we can now run the spiritual descendant of that code in a Docker container on any modern computer speaks to Fortran’s remarkable longevity.

Next Steps

Continue to Variables and Data Types to learn about storing numbers and text in Fortran.

Last updated: