Fortran Hello, world


Example

Any Fortran program has to include end as last statement. Therefore, the simplest Fortran program looks like this:

end

Here are some examples of "hello, world" programs:

print *, "Hello, world" 
end

With write statement:

write(*,*) "Hello, world"
end

For clarity it is now common to use the program statement to start a program and give it a name. The end statement can then refer to this name to make it obvious what it is referring to, and let the compiler check the code for correctness. Further, all Fortran programs should include an implicit none statement. Thus, a minimal Fortran program actually should look as follows:

program hello
  implicit none
  write(*,*) 'Hello world!'
end program hello

The next logical step from this point is how to see the result of the hello world program. This section shows how to achieve that in a linux like environment. We assume that you have some basic notions of shell commands, mainly you know how to get to the shell terminal. We also assume that you have already setup your fortran environment. Using your preferred text editor (notepad, notepad++, vi, vim, emacs, gedit, kate, etc.), save the hello program above (copy and paste) in a file named hello.f90 in your home directory. hello.f90 is your source file. Then go to the command line and navigate to the directory(home directory?) where you saved your source file, then type the following command:

>gfortran -o hello hello.f90

You just created your hello world executable program. In technical terms, you just compiled your program. To run it, type the following command:

>./hello

You should see the following line printed on your shell terminal.

> Hello world!

Congratulations, you just wrote, compiled and ran the "Hello World" program.