Fortran Hello World
Every Fortran executable has exactly one main program. It starts with program <name> and finishes with end program <name>. Everything between is the program body: first the declarations, then the statements that do the work.
program hello
implicit none
print *, 'Hello, World!'
end program hello Hello, World!
Let's take it line by line:
| Line | Meaning |
|---|---|
program hello |
starts the main program and gives it a name (any valid identifier) |
implicit none |
switches off an old rule that guesses variable types from their first letter; always write it (see the implicit none lesson) |
print *, 'Hello, World!' |
prints a list of values; * means "choose the layout for me" (list-directed output) |
end program hello |
ends the program; repeating the name is optional but helps readers |
Why the leading space?
Look closely at the output above: there is a space before Hello. List-directed output (print *) is allowed to choose its own spacing, and gfortran always starts each line with one blank. Historically the first character of a line controlled the line printer (a space meant "advance one line"), and the habit stuck.
To control the output exactly, give print a format instead of *. The format '(a)' means "print one character string as is":
program hello_formatted
implicit none
print '(a)', 'Hello, World!'
print '(a)', 'Fortran says hi.'
end program hello_formattedHello, World!
Fortran says hi.
We'll use list-directed print * while learning the basics, because it works with any type. Once formats are taught you'll see them almost everywhere in this course, since they give the same result on every compiler.
Printing several values
print takes a comma-separated list of items, which may mix text and numbers:
program several_items
implicit none
print *, 'Two plus three is', 2 + 3
print *, 'Pi is roughly', 3.14159
print *, 'Is 7 > 5?', 7 > 5
end program several_items Two plus three is 5
Pi is roughly 3.14159012
Is 7 > 5? T
Notice three things that surprise beginners:
- Integers are printed right-aligned in a wide field (here 12 characters).
3.14159came out as3.14159012: a defaultrealstores about 7 significant digits, and gfortran shows 9 digits, revealing the nearest value a binary float can hold. The kinds lesson shows how to get more precision.- Logical values print as
TandF.
Note: This spacing is compiler-specific. Intel Fortran, NAG and gfortran all print list-directed output differently. Use it for quick checks, and formats for anything a person or another program will read.
Comments and the structure of a program
A ! starts a comment that runs to the end of the line. A slightly longer program shows the usual layout: declarations at the top, executable statements below.
program greet
implicit none
! --- declarations ---
character(len=*), parameter :: name = 'Ada'
integer :: year
! --- statements ---
year = 2025
print '(a, a, a)', 'Welcome, ', name, '!' ! three strings, side by side
print '(a, i0)', 'The year is ', year ! i0 = integer, minimum width
end program greetWelcome, Ada!
The year is 2025
Compiling on your own machine
gfortran hello.f90 -o hello # compile
./hello # run
gfortran -Wall -std=f2018 -fcheck=all hello.f90 -o hello # warnings + runtime checks while learning-fcheck=all makes the program stop with a clear message if you index outside an array or similar, which is extremely useful while learning.
Test yourself
Which statement ends the main program named demo?
stop demoend program demoreturn demofinish demo
What does the * in print *, x mean?
- Multiply x
- Print to every device
- List-directed output: the compiler chooses the layout
- Print x as a pointer
How do you print a string with no leading blank?
print *, 'Hi'print '(a)', 'Hi'print 'Hi'write *, 'Hi'