3.3. Source Form

A Fortran program consists of statements, comments, and INCLUDE lines. This collection of statements, comments, and INCLUDE lines is called source text. A statement consists of one or more lines of source text and is constructed from low-level syntax.

The lines within a program unit (except comment lines) and the order of the lines are, in general, significant.

Because all program units terminate with their own END statement, lines following such an END statement are never part of the preceding program unit; they are part of the program unit that follows. END statements can be continued.

Note: The Fortran standard does not describe END statement continuation.

There are two source forms for writing source text: free source form and fixed source form.

Note: The Fortran standard has declared fixed source form to be obsolescent. The preferred alternative is free source form.

Fixed source form is the default for Fortran source files with a .f or .F suffix. Free source form is the default source for Fortran source files with a .f90, .F90, .ftn, or .FTN suffix. The compiler allows you to use the FIXED and FREE compiler directives to switch from one source form to the other within a program unit. The Cray Fortran command line allows you to override the source form implied by the input file suffix. See theCray Fortran Compiler Commands and Directives Reference Manual for information about the ftn(1) command line and the FIXED and FREE compiler directives. Section 3.4, describes a way to write Fortran statements so that the source text is acceptable to both free and fixed source forms.

Note: The Fortran standard does not describe compiler directives or command lines.

Characters that form the value of a character literal constant, a Hollerith constant, or a character string edit descriptor (quotation mark, apostrophe, or H edit descriptor) are in a character context. The characters in a character context do not include the delimiters used to indicate the beginning and end of the character constant or string. Also, the ampersands (&) in free source form, which are used to indicate that a character string is being continued and used to indicate the beginning of the character string on the continued line, are never part of the character string value and thus are not in character context.

Note: The Fortran standard does not describe Hollerith constants.

The rules that apply to characters in a character context are different from the rules that apply to characters in other contexts. For example, blanks are always significant in a character context but are never significant in other parts of a program written using fixed source form. The following code fragment illustrates this:

CHAR = CHAR1 // "Mary K. Williams"
! The blanks within the character string
! (within the quotation marks) are significant.
! The next two statements are equivalent
! in fixed source form.
DO2I=1,N
DO 2 I = 1, N

Comments can contain any printable character.

3.3.1. Free Source Form

In free source form, the only restriction on statement positioning within a line is that the line itself cannot contain more than 132 characters. A blank character is significant and may be required to separate lexical tokens.

Blank characters are significant everywhere, but a sequence of blank characters outside a character context is treated as a single blank character. They can be used freely between tokens and delimiters to improve the readability of the source text. For example, the following two statements are interpreted identically:

SUM=SUM+A(I)
SUM = SUM + A (I)

Each line can contain from 0 through 132 characters.

The exclamation mark (!), not in character context, indicates the beginning of a comment that ends with the end of the line. A line can consist of nothing but a comment. Comments, including the !, are ignored and do not alter the interpretation of statements in any way.

The Cray Fortran Compiler supports compiler directives. Compiler directives are lines inserted into source code that specify actions to be performed by the compiler. Compilers other than the Cray Fortran Compiler may treat compiler directive lines as comments. For more information on compiler directives, see the Cray Fortran Compiler Commands and Directives Reference Manual.

A line whose first nonblank character is an exclamation mark is called a comment line.

The following is an example of a comment line:

! Begin the next iteration.

The following is an example of a statement with a trailing comment:

ITER = ITER + 1   ! Begin the next iteration.

An ampersand (&), not in a character context, is a continuation symbol and must be followed by one of the following:

The following is an example of a continued line and a continuation line:

FORCE = G * MASS1 *  & ! This is a continued line.
       MASS2 / R**2 ! This is a continuation line.

The Cray Fortran Compiler allows a statement to be continued with up to 99 continuation lines.

Note: If you are using free source form, the Fortran standard allows no more than 39 continuation lines within a statement.

No line can contain an ampersand as the only nonblank character before an exclamation mark. Comment lines cannot be continued. That is, the ampersand as the last character in a comment is part of the comment and does not indicate continuation.

A line with only blank characters or with no characters is treated as a comment line.

More than one statement or partial statement can appear on a line. The statement separator is the semicolon (;), provided it is not in a character context. Multiple successive semicolons on a line with or without blanks intervening are considered as a single separator. The end of a line is also a statement separator, but a semicolon at the end of a line that is not part of a comment is considered as a single separator. Essentially, a null statement is a legal Fortran statement. The following statements show use of the semicolon:

! The semicolon is a statement separator.
X = 1.0;  Y = 2.0
! However, the semicolon below, at the end of a
! line, is not treated as a separator and is
! ignored.
Z = 3.0;
! Also, consecutive semicolons are treated as one
! semicolon, even if blanks intervene.
Z = 3.0; ; W = 4.0

A label can appear before a statement, provided that it is not part of another statement, but it must be separated from the statement by at least one blank. Examples:

10 FORMAT(10X,2I5)                ! 10 is a label
   IF (X == 0.0) 200 Y = SQRT(X)  ! Label 200 is
                                  ! not allowed.

Any printable character can be used in character literal constants and character string edit descriptors.

The Cray Fortran Compiler supports only the ASCII character set. The Fortran Language Reference Manual, Volume 3, describes the ASCII character set.

3.3.1.1. The Ampersand (&) As a Continuation Symbol

The ampersand (&) is used as the continuation symbol in free source form. If it is the last nonblank character in a line after any comments are deleted and it is not in a character context, the statement is continued on the next line that does not begin with a comment. If the first nonblank character on the continuing line is an ampersand, the statement continues after the ampersand; otherwise, the statement continues with the first position of the line. The ampersand or ampersands used as the continuation symbols are not considered part of the statement. For example, the following statement takes two lines (one continuation line) because it is too long to fit on one line:

STOKES_LAW_VELOCITY = 2 * GRAVITY * RADIUS ** 2 *  &
      (DENSITY_1 - DENSITY_2) / (9 * COEFF_OF_VISCOSITY)

The leading blanks on the continued line are included in the statement and are allowed in this case because they are between lexical tokens.

The double ampersand convention must be used to continue a name, a character constant, or a lexical token consisting of more than 1 character split across lines. The following statement is the same statement as in the previous example:

STOKES_LAW_VELOCITY = 2 * GRAVITY * RADIUS ** 2 * (DEN&
      &SITY_1 - DENSITY_2) / (9 * COEFF_OF_VISCOSITY)

However, splitting names across lines makes the code difficult to read and is not recommended.

Ampersands can be included in a character constant. Only the last ampersand on the line is the continuation symbol, as illustrated in the following example:

LAWYERS = "Jones & Clay & &
&Davis"

The value of this constant is Jones & Clay & Davis. The first two ampersands are in a character context; they are part of the value of the character string.

END statements cannot be continued.

3.3.1.2. Blanks As Separators

Blanks in free source form cannot appear within tokens, such as names or symbols consisting of more than 1 character, but blanks can be used freely in format specifications. For example, blanks cannot appear between the characters of multicharacter operators such as ** and .NE.. Format specifications are an exception because blanks can appear within edit descriptors such as BN, SS, or TR.

The Cray Fortran Compiler treats tabs and single blanks as equivalent in free source form, except in character literal strings.

Note: The Fortran standard does not allow tabs in Fortran source files.

A blank must be used to separate a statement keyword, name, constant, or label from an adjacent name, constant, or label. For example, the blanks in the following statements are required:

INTEGER SIZE
PRINT 10,N
DO I=1,N

Adjacent keywords require a blank separator in some cases (for example, CASE DEFAULT). In other cases, two adjacent keywords can be written either with or without intervening blanks (for example, BLOCK DATA).

Blank separators are optional in the following keywords:

  • BLOCK DATA

  • DOUBLE PRECISION

  • ELSE IF

  • END ASSOCIATE

  • END BLOCK DATA

  • END DO

  • END FILE

  • END IF

  • END INTERFACE

  • END MODULE

  • END PROGRAM

  • END SELECT

  • END SUBROUTINE

  • END TYPE

  • END ENUM

  • END WHERE

  • GO TO

  • IN OUT

  • SELECT CASE

Blank separators are mandatory in the following keywords:

  • CASE DEFAULT

  • DO WHILE

  • IMPLICIT implicit_spec_list

  • IMPLICIT NONE

  • INTERFACE ASSIGNMENT

  • INTERFACE OPERATOR

  • MODULE PROCEDURE

  • RECURSIVE FUNCTION

  • RECURSIVE SUBROUTINE

  • RECURSIVE type_spec

  • type_spec FUNCTION

  • type_spec RECURSIVE

Blanks are not mandatory in all cases, but blank separators between statement keywords make the source text more readable and clarify the statements. Generally, if common rules of English text are followed, everything will be correct. For example, blank separators in the following statements make them quite readable, and the blanks in DOUBLE PRECISION and END FUNCTION are optional:

RECURSIVE FUNCTION F(X)
DOUBLE PRECISION X
END FUNCTION F

3.3.1.3. Sample Program, Free Source Form

The following is a sample program in free source form. Note that the numbers and the line at the top are not part of the program; the vertical bars to the left of the program are also not part of the program. These graphics are included to show the columns this program uses in free source form.

 123456789.......  
 -----------------------------------------------------
|PROGRAM LEFT_RIGHT
| REAL  X(5), Y(5)
|       ! Print arrays X and Y
|       PRINT 100, X, Y
|       100 FORMAT (F10.1, F10.2, F10.3, F10.4,  &
|                   F10.5)
|   . . .
|END

3.3.2. Fixed Source Form

Fixed source form is position oriented on a line using the historical Fortran conventions for position that were used on punched cards.

Note: The Fortran standard has declared fixed source form to be obsolescent. The preferred alternative is free source form.

By default, statements or parts of statements must be written between positions 7 and 72, inclusive. The Cray Fortran command line includes options that allow you to specify different line lengths. For information on specifying different line lengths, see the Cray Fortran Compiler Commands and Directives Reference Manual.

Regardless of the command line specification, character positions 1 through 6 are reserved for special purposes.

Note: The Fortran standard does not allow a compiler to recognize characters beyond column 72.

Blanks are not significant in fixed source form except in a character context. For example, the following two statements are identical:

D O  10  I = 1, L O O P E N D
DO 10 I = 1, LOOPEND

A C or * in position 1 identifies a comment. In this case, the entire line is a comment and is called a comment line. A ! in any position except position 6 and not in a character context indicates that a comment follows to the end of the line. Comments are not significant.

A line with only blank characters or with no characters is treated as a comment line.

Multiple statements on a line are separated by one or more semicolons. Semicolons can occur at the end of a line, and these are ignored.

Any character (including ! and ;) other than blank or 0 in position 6 indicates that the line is a continuation of the previous line. Such a line is called a continuation line. The text on the continuation line begins in position 7. There can be no more than 99 continuation lines for one statement in fixed source form. The first line of a continued statement is called the initial line.

Note: If you are using fixed source form, the Fortran standard allows no more than 19 continuation lines within a statement.

Statement labels can appear only in positions 1 through 5. A label can appear only on the initial line of a continued statement. Thus, positions 1 through 5 of continuation lines must contain blanks.

The Cray Fortran Compiler allows you to continue an END statement, as follows:

 E
&N
&D

Note: The Fortran standard states that an END statement must not be continued.

The characters END can appear as the only characters on the initial line of a statement, as follows:

 END
&FILE(10)

Note: The Fortran standard states that END cannot be an initial line of a statement other than an END statement.

Any character from the compiler character set (including graphic and control characters) can be used in character literal constants and character edit descriptors. Although the Fortran standard permits a processor to limit the use of control characters (such as the newline) to such character contexts, the Cray Fortran Compiler imposes no such limitation.

3.3.2.1. Tab Character

You can substitute the tab character for spaces at the beginning of a line, but it is not actually converted to spaces. The compiler uses the tab as follows:

  • If a tab is the first character on a line, the next character determines how the line is interpreted. A nonzero digit indicates a continuation line. Otherwise, this line is the initial line of a statement.

  • A statement label, if present, must precede the first tab.

  • The compiler treats the tab character in a statement the same way it treats a blank character.

A tab character is not converted to spaces, so the exact visual placement of tabbed statements depends on the utility you use to edit or display text.

Note: A tab counts as 1 character even though it may expand to more than one space in the listing or editor. Thus, statements that include tabs may appear to have data beyond column 72 (or 80) in the editor or listing.

3.3.2.2. D or d character (EXTENSION)

The Cray Fortran Compiler allows a D or d character to occur in column one. Typically, the compiler treats a line with a D or d character in column one as a comment line. When the -ed command line option is in effect, however, the compiler replaces the D or d character with a blank and treats the rest of the line as a source statement. This can be used, for example, for debugging purposes if the rest of the line contains a PRINT statement.

This functionality is controlled through the -ed and -dd options on the compiler command line. For more information on these options, see the ftn(1) man page.

Note: The Fortran standard does not describe using D or d characters in column 1.

3.3.2.3. Sample Program, Fixed Source Form

The following is a sample program in fixed source form. Note that the numbers and the line at the top are not part of the program; the vertical bars to the left of the program are also not part of the program. These graphics are included to show the columns this program uses in fixed source form.

 123456789.....
---------------------------------------------
|      PROGRAM LEFT_RIGHT
|      REAL  X(5), Y(5)
|C     Print arrays X and Y
|      PRINT 100, X, Y
|  100 FORMAT (F10.1, F10.2, F10.3, F10.4,
|     1        F10.5)
|         . . .
|      END