3. FORTRAN 77 Basics

A Fortran program is just a sequence of lines of text. The text has to follow a certain structure to be a valid Fortran program. We start by looking at a simple example:

      program circle
      real r, area

c This program reads a real number r and prints
c the area of a circle with radius r.

      write (*,*) 'Give radius r:'
      read  (*,*) r
      area = 3.14159*r*r
      write (*,*) 'Area = ', area


The lines that begin with a "c" are comments and have no purpose other than to make the program more readable for humans. Originally, all Fortran programs had to be written in all upper-case letters. Most people now write lower-case since this is more legible, and so will we. You may wish to mix case, but Fortran is not case-sensitive, so "X" and "x" are the same variable.

Program organization

A Fortran program generally consists of a main program (or driver) and possibly several subprograms (procedures or subroutines). For now we will place all the statements in the main program; subprograms will be treated later. The structure of a main program is:

      program name




In this tutorial, words that are in italics should not be taken as literal text, but rather as a description of what belongs in their place.

The stop statement is optional and may seem superfluous since the program will stop when it reaches the end anyway, but it is recommended to always terminate a program with the stop statement to emphasize that the execution flow stops there.

You should note that you cannot have a variable with the same name as the program.

Column position rules

FORTRAN 77 is not a free-format language, but has a very strict set of rules for how the source code should be formatted. The most important rules are the column position rules:

1Blank, or a "c" or "*" for comments
1-5Statement label (optional)
6Continuation of previous line (optional; see below)
73-80Sequence number (optional, rarely used today)

Most lines in a FORTRAN 77 program starts with 6 blanks and end before column 72, i.e. only the statement field is used.


A line that begins with the letter "c" or an asterisk in the first column is a comment. Comments may appear anywhere in the program. Well-written comments are crucial to program readability. Commercial Fortran code often contains about 50% comments. You may also encounter Fortran programs that use the exclamation mark (!) for comments. This is not a standard part of FORTRAN 77, but is supported by several FORTRAN 77 compilers and is explicitly allowed in Fortran 90. When understood, the exclamation mark may appear anywhere on a line (except in positions 2-6).


Sometimes, a statement does not fit into the 66 available columns of a single line. One can then break the statement into two or more lines, and use the continuation mark in position 6. Example:

c The next statement goes over two physical lines

      area = 3.14159265358979

     +       * r * r

Any character can be used instead of the plus sign as a continuation character. It is considered good programming style to use either the plus sign, an ampersand, or digits (using 2 for the second line, 3 for the third, and so on).

Blank spaces

Blank spaces are ignored in FORTRAN 77. So if you remove all blanks in a FORTRAN 77 program, the program is still acceptable to a compiler but almost unreadable to humans.

Copyright © 1995-7 by Stanford University. All rights reserved.

[ Index ]