Fortran/Hello world

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Part of the Fortran WikiBook

Contents

[edit] Hello_World.f

Below is a simple Fortran program. You can paste this into a text editor such as Notepad, Emacs or Kate. Do not use a word processor (such as Microsoft Word) as Word contains too much formating for a compiler to deal with, and does not save files with the proper extension.

Give the file a name such as hello.f - common Fortran file extensions are .f, .FOR, .for, .f77, .f90 and .f95. You may also use .F, .fpp and .FPP (which support Preprocessing).

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

or the archaic but shorter form:

      PRINT *,"Hello World!"
      END

Fixed format source code needs 6 spaces before code begins, but the free source form of Fortran 90 does not.

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

PrOgRaM HELLo
   prINT*,"Hello World!"
End ProgRAm HeLlO

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

[edit] Compiling

[edit] Unix / Linux

How to compile depends on your compiler. There are two freely available

  • G95 This compiler is compliant with the Fortran 95 standard (though some Fortran 2003 standards are supported) and can be used with
g95 hello.f90 -o hello

and press enter.

  • gfortran is part of the GNU compiler collection and is compliant with the Fortran 95 standard (though some Fortran 2003 standards are supported) and can be used with
gfortran  hello.f -o hello

and press enter. The program in both the examples is called "hello", which you can run by typing:

hello

or

./hello

[edit] Windows

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. 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

[edit] OpenVMS

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.