TExcelTAD
Home › Fortran › Fortran Hello World

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.

fortran · try it yourself
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":

fortran · try it yourself
program hello_formatted
  implicit none
  print '(a)', 'Hello, World!'
  print '(a)', 'Fortran says hi.'
end program hello_formatted
Hello, 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:

fortran · try it yourself
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:

  1. Integers are printed right-aligned in a wide field (here 12 characters).
  2. 3.14159 came out as 3.14159012: a default real stores 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.
  3. Logical values print as T and F.

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.

fortran · try it yourself
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 greet
Welcome, Ada!
The year is 2025

Compiling on your own machine

bash
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?

  1. stop demo
  2. end program demo
  3. return demo
  4. finish demo

What does the * in print *, x mean?

  1. Multiply x
  2. Print to every device
  3. List-directed output: the compiler chooses the layout
  4. Print x as a pointer

How do you print a string with no leading blank?

  1. print *, 'Hi'
  2. print '(a)', 'Hi'
  3. print 'Hi'
  4. write *, 'Hi'