Fortran/Hello world

Simple Fortran program

edit

Below is a simple Fortran program. You can paste this into a text editor (such as Emacs or Vim). Source code must be in a plain text file, so don't use a word processor (such as Microsoft Word), because its native format is usually not plain text, or otherwise contains special formatting data. Give the file a name such as hello.f90. The filename extension .f90 is conventionally used for modern Fortran source code. Other common Fortran file extensions are .f, .FOR, .for, .f77, .f90 and .f95, which can denote the use of old or specific Fortran standards. You may also use .F, .fpp and .FPP for files that support Preprocessing.

program hello
    print *, "Hello World!"
end program

Because Fortran is case insensitive, one could just as easily write the first 'hello' program as:

Program Hello
    Print *, "Hello World!"
End Program

The only case sensitive part of this program is what contained in the quotation marks of the print statement. ("Hello World!")

Compiling

edit

Unix

edit

There are several Fortran compilers available for Unix. Among the most popular are:

  • GNU Fortran compiler from the GCC, which is a fork of G95. Invocation:
    gfortran -o hello hello.f90
    
    Note: the GNU Fortran compiler uses the FORTRAN 77 standard by default, so the input file must have the .f90 (or of later standard) suffix, so that the compiler can guess the desired standard automatically. Alternatively you can supply the -ffree-form option with the usual .f suffix to enable free-form format instead of the fixed-form format used by the FORTRAN 77 standard.
  • Fortran 95 optimizing compiler from Oracle Solaris Studio. Invocation:
    f95 -o hello hello.f90
    

Once the program is compiled and linked, you may execute it:

./hello

Windows

edit

On Windows, you will need to install a compiler. You may also want to install an IDE for that compiler, which acts as an editor and allows you to compile the program more easily.

When you have a compiler, open a command prompt (MS-DOS prompt). This looks like

 C:\>

or something similar. At the prompt, you need to move into the folder containing the .f90 file. Then, to compile, type

 f95 hello.f90 -o hello.exe

This assumes the compiler is called "f95". The Intel compiler is typically "ifort". You may need to specify where this is, for example if it's in Program Files\Compiler, use:

 "C:\Program Files\Compiler\f95" hello.f90 -o hello.exe

Alternatively, you could install a text editor with support for Fortran compilers. Such as SciTE [1] The above commands produce an executable called hello.exe - to run this, just type

 hello

at the command prompt

OpenVMS

edit

On VMS you will need the DEC Fortran90 compiler installed and licenses loaded. This is available as part of the hobbyist project. These commands work for Fortran on both Alpha and VAX.

To compile, type the following at the DCL prompt:

$ FORTRAN HELLO.F

To link the file to the Run-Time Lib (RTL) type the following:

$ LINK HELLO.OBJ

To Run the executable image, type the following:

$ RUN HELLO.EXE
Hello World!
$

Enjoy all that VMS and Fortran offers.