fortranfortran90fortran77

Are spaces no longer allowed in variable names


When I was in Uni, in Fortran IV, we used to be able to write a program like this

      program main
      integer his days, her days, total of days

      his days = 15
      her      days = 25
      total of days = his     days + herdays

      write(5,100) total of days
100   format(1X, I10)
      stop
      end program

I tried building this as an F77 program and it compiled and ran. I don't know if spaces were explicitly allowed in F77 but I remember writing programs with spaces in variables in Fortran IV (66). This builds and runs in gfortran, Powerstation 4, Silverfrost and an old g77 compiler. Presumably it will work on the IVF compiler too (since IVF was Powerstation4 in a previous life). I don't have acces to one right now. If I tell the compiler it is an F90/95... program, it doesn't even compile.

f90test.f90:2:17:

   integer his days, her days, total of days
             1
Error: Syntax error in data declaration at (1)
f90test.f90:4:6:

   his days = 15
  1
Error: Unclassifiable statement at (1)
f90test.f90:5:6:

   her      days = 25
  1
Error: Unclassifiable statement at (1)
f90test.f90:6:6:

   total of days = his     days + herdays
  1
Error: Unclassifiable statement at (1)
f90test.f90:8:21:

   print *, total of days
                 1
Error: Syntax error in PRINT statement at (1)

The two questions are

  1. were spaces allowed in variable names in F77? All the references I can find don't actually say that spaces were allowed or maybe the authors didn't know that spaces were allowed
  2. were spaces in variable names explicity removed in F90?

Solution

  • When you use the .f90 file extension, it is treated like free-form source file. In the free-form the whitespace does matter. This source form was introduced in Fortran 90. For backward compatibility, the keywords are available both in the split and the non-split way (GOTO and GO TO, END IF and ENDIF). However, identifiers (names) are not allowed to contain spaces in this source form.

    Use .f or .for for fixed-form source files, if you want to use it. It is better to consult your compiler's manual which extensions it allows. The fixed source form is still a valid source form today, although marked obsolete in Fortran 2018. Spaces do not have any significance in the fixed-source form even if it is a Fortran 90 or even Fortran 2018 code.

    Note that the compilers today compile most codes as some new standard (2003, 2008 or 2018), no matter what the extension is. Notably, the extension .f90 is not regarded as Fortran 90, it just marks the source file form in most (or all?) compilers. See also Correct suffix for Fortran 2003 source file - Intel Fortran compiler This naming convention is not defined by the standard, but pretty universal.