A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Hello World in Fortran page! Here, you'll find the source code for this program as well as a description of how the program works.
program helloworld
implicit none
print '(A)', 'Hello, World!'
end program helloworld
Hello World in Fortran was written by:
This article was written by:
If you see anything you'd like to change or update, please consider contributing.
Let's dig into Hello World in Fortran, line by line.
First, we have the program statement, which starts the main program and
gives it a name, helloworld. Every Fortran executable has exactly one main
program.
Next is implicit none. Without it, Fortran guesses the type of any
undeclared variable from its first letter: names starting with I through N
are integers, and everything else is real. That rule dates back to the
earliest Fortran, and it turns a typo into a brand new variable instead of an
error. implicit none switches the rule off, so every variable has to be
declared. This program has no variables, but the line is a good habit, and
most modern Fortran code starts with it.
Then comes the line that does the work:
print '(A)', 'Hello, World!'
The print statement writes to standard output. Its first argument is a
format: '(A)' says to write one character value exactly as it is. We could
have written print *, 'Hello, World!' instead, where * means "list-directed"
output and the compiler picks the layout. Many compilers, gfortran among them,
then start the line with a space, so the explicit format keeps the output to
exactly Hello, World!.
Finally, end program helloworld closes the main program. Repeating the name
is optional, but it makes the end of a long program easy to match with its
start.
If we just want to see it run, we can paste the solution into an online Fortran compiler and press Run.
To run it locally, we need a Fortran compiler. The free option is gfortran, which is part of GCC, and the Fortran community keeps install instructions for Windows, macOS and Linux. With gfortran on our path and a copy of the solution, we can compile and run it from the folder that holds it:
gfortran -o hello-world hello-world.f95
./hello-world
On Windows, the second command is hello-world.exe. The .f95 extension
tells gfortran the source uses the free-form layout of Fortran 90 and later,
so no special flags are needed. When the program runs, Hello, World! prints
to the console.