Your first Fortran program
This lesson writes the smallest complete Fortran program, builds it, runs it, and then takes it apart one line at a time. Nothing here is decoration: every line has a job, and the last section shows what the compiler says when one of them is missing.
The file
Four lines are enough. Save them as hello.f90 in a folder of its own, which keeps the build output of later exercises away from this one.
The file hello.f90:
program hello
implicit none
print *, "Hello, Fortran!"
end program hello
Line by line
The first line opens a program unit and gives it the name hello. The name is not the file name and does not have to match it; it is the name the compiler uses in its messages and the name a linker would see. A name in Fortran is a letter followed by letters, digits and underscores, and the standard guarantees the first 63 characters.
The second line is implicit none. Without it, Fortran types an undeclared name from its first letter: i to n is an integer, everything else is a real. That rule made sense in 1957, when a program was written on cards and every declaration was a card, and it is a source of bugs now, so the second line of every unit this course writes turns it off. The full story is in the lesson on IMPLICIT NONE.
The third line prints. print * means two things: write to standard output, and format the values in list-directed form, which is the compiler's choice of width and number of digits. The * is not a wildcard here; it is a placeholder for a format, and a later lesson replaces it with a real one.
The fourth line closes the unit. It repeats the name, and that repetition is free documentation: a file with four units in it stays readable. Fortran allows the bare end, and this course writes end program name for the same reason it writes the other end statement in full.
Indentation is not part of the language in free form: the two spaces in front of the body are a convention, and every Fortran program in the wild uses it. The compiler ignores them.
Build and run
gfortran -Wall -Wextra -std=f2018 hello.f90 -o hello
./hello
Hello, Fortran!
The space before the text is part of the output. List-directed output starts every record with one blank, a rule that goes back to the line printers of the 1960s, and the standard still keeps it. A program that compares its output to a string byte for byte has to allow for it, and a reader who sees the blank knows the program used print * rather than a format.
The build writes one file, hello, next to the source. It is an ordinary executable: ./hello runs it, file hello names the platform, and ldd hello lists the C library and the Fortran runtime it links against. Nothing about it needs the source or the compiler afterwards, which is what a compiled language buys.
The exit status
A program that reaches its end statement returns the status 0, which the shell reports as success. A build script, a test runner and make all read that number and nothing else, so it is worth knowing where it comes from.
./hello
echo $?
Hello, Fortran!
0
The status leaves the program through stop and error stop, which are the subjects of a later lesson. For now, the rule is that a program which ends normally says 0, and that a value other than 0 means something went wrong.
PRINT and WRITE
print * is a shorthand. The full statement names the unit and the format: write (*, *) writes to the default output unit in list-directed form. The two programs print the same line, and a reader meets both forms in other people's code.
The file hello2.f90:
program hello2
implicit none
write (*, *) "Hello, Fortran!"
end program hello2
gfortran -Wall -Wextra -std=f2018 hello2.f90 -o hello2
./hello2
Hello, Fortran!
Use print * while learning and write (*, *) when the program grows: the second form generalises, because the first asterisk can name a unit such as a file or the standard error stream. The formatted output lesson replaces the second asterisk with a format string.
The wrong END
The last line repeats the name of the unit, and the compiler checks that the two agree. Write a different name there and the build stops with an error that names both.
The file wrongend.f90:
program noend
implicit none
print *, "no end here"
end program other
gfortran -Wall -Wextra -std=f2018 wrongend.f90 -o wrongend
wrongend.f90:4:17:
4 | end program other
| 1
Error: Expected label 'noend' for END PROGRAM statement at (1)
The caret points at the name, and the message says which one the compiler wanted. Because the unit never closed, the front end also reports f951: Error: Unexpected end of file in 'wrongend.f90' after this line: that is the message a file gets when no END statement appears at all, and it comes from f951, the name of the Fortran front end inside the GCC compiler. A build log searched for the word gfortran finds nothing, because the interesting lines carry the front end's name.
Write end program noend and the program builds. Nothing else changed, which is the lesson: the end statement is not a formality that a file may drop.
Exercises
- Change the greeting to your own name, rebuild, and run. Which parts of the output change?
- Rename the program unit to main and leave the file name alone. Does the build change?
- Delete the implicit none line and add i = 1 before the print. Does the program build, and what does it print next to the greeting?
- Print two values in one statement, print *, 1, 2.5. How many spaces separate them, and why?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the first line of a Fortran program unit do?
Answer
It opens the main program and gives it a name
PROGRAM name opens the main unit; the name is used in compiler messages.
-
Why is
implicit nonewritten in the first line of the body?Answer
It turns off the first-letter type rule, so every name must be declared
Without it a name typed from its first letter is created silently.
-
What is the difference between
print *andwrite (*, *)?Answer
Nothing at run time: the second is the full statement the first abbreviates
print * is shorthand for write to the default unit in list-directed form.
-
Why does the output start with a space?
Answer
List-directed output starts every record with one blank
The standard keeps the blank that line printers needed in the 1960s.
-
What is the exit status of a program that reaches its end statement?
Answer
0
Normal termination returns 0, which the shell reports as success.
-
What does this program print?
program p implicit none print *, 6 / 4 end program pAnswer
1
Both operands are integers, so the division truncates toward zero and the value is 1.
-
What does the compiler say about
end program otherin a unit namednoend?Answer
Error: Expected label 'noend' for END PROGRAM statement at (1)
The compiler checks that the name on END PROGRAM matches the one on PROGRAM.
-
Must the program name match the file name?
Answer
No: the name is used in messages, the file name in the build command
gfortran reads whatever file it is given and the program name is a separate identifier.
-
Which line of
hello.f90keeps the program from printing a guessed variable?Answer
implicit none
Without implicit none an undeclared name would be typed from its first letter.
-
What does
-o helloin the build command do?Answer
It names the produced binary hello
Without -o the binary would be called a.out.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev