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:
| |
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:
| |
Running Locally
If you have gfortran installed:
| |
Installing gfortran
macOS (Homebrew):
| |
Ubuntu/Debian:
| |
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
| |
The '(A)' format specifier outputs the string without the leading space.
Classic FORTRAN 77 Style (Fixed-Form)
| |
This fixed-form style requires specific column positions. Modern code should use free-form.
Key Concepts
- Fortran is compiled - Source code (
.f90) is compiled to a native executable - Program structure - Every program needs
program nameandend program name - Case insensitive -
PRINT,Print, andprintare identical implicit noneis essential - Always include it to catch typos- File extensions matter - Use
.f90for free-form,.for.f77for fixed-form
Compiler Options
Useful gfortran flags for development:
| |
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.