Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

spaces not ignored in free format

John_M_12
Beginner
1,450 Views

It seems that spaces matter in free format. For example, the following is an acceptable fixed-format file that works fine.

        program spacing
        ii = 11 111
        jj = 22 222
        k k = ii + jj
        w r i t e(*,*) k k
        e n d

If this program is saved as spacing.f, it compiles and runs fine. If it is saved as spacing.f90, then ifort finds syntax errors on every line except the PROGRAM statement. So, free format is less free than fixed format in this regard. At least the spaces in "end if" and "end do" are still allowed and optional.

Can anyone confirm that not ignoring spaces in free-format files is the Fortran standard? Some searching hasn't turned up anything addressing spaces in free format.

0 Kudos
3 Replies
Steven_L_Intel1
Employee
1,450 Views

Yes, spaces matter in free format. Why did you think otherwise?

3.3.2.2 Blank characters in free form
1 In free source form blank characters shall not appear within lexical tokens other than in a character context or in
a format specification. Blanks may be inserted freely between tokens to improve readability; for example, blanks
may occur between the tokens that form a complex literal constant. A sequence of blank characters outside of a
character context is equivalent to a single blank character.

2 A blank shall be used to separate names, constants, or labels from adjacent keywords, names, constants, or labels.

The Intel Fortran Language Reference says:

Blank characters are significant in free source form. The following are rules for blank characters:

Blank characters must not appear in lexical tokens, except within a character context. For example, there can be no blanks between the exponentiation operator **. Blank characters can be used freely between lexical tokens to improve legibility. 

0 Kudos
John_M_12
Beginner
1,450 Views

Why did I think otherwise? Because spaces are ignored in fixed format, and I didn't expect something called "free format" to be more constrained than something called "fixed format." I like Fortran's style of ignoring case, and ignoring spaces in fixed format is another bit of comfortable looseness over something that shouldn't matter. It had been useful at times to write a number as 3.14159 26535 89793 23846 rather than 3.14159265358979323846 or 299 792 458 rather than 299792458. Some Fortran77 programs I've used, written by others, had spaces between letters to draw the eye to something, a bit of extra bolding when everything was typed in upper-case. Humans have a variety of uses for white space. Does making blank characters significant in free format serve some need of the compiler that doesn't exist for fixed format?

At any rate, the standard is what it is, and I thank you for quoting it.

0 Kudos
Steven_L_Intel1
Employee
1,450 Views

Yes, making blanks significant is a major contributor towards code correctness. There have been many documented cases of serious coding errors caused by nonsignificant blanks. A classic one is:

DO 10 I=1.10

In fixed form this assigns 1.10 to a (probably) implicitly declared REAL variable DO10I. In free-form it is invalid.

0 Kudos
Reply