Line continuation of strings in Fortran

12,719

Solution 1

This should work too:

PRINT *, 'I am a new learner of &
     &fortran'

That is, character literals can be continued across line breaks but each continuation line must have an ampersand in the first non-blank position.

Solution 2

The way statement continuation works in free-form source is to transform the statement of the question to

print *, 'I am a new learner of'      'fortran'

This isn't a valid thing. Naturally, one could write

print *, 'I am a new learner of'//' fortran'

or

print *, 'I am a new learner of', 'fortran'

and possibly see much the same effect.

However, as noted in the other answers, literal characters may be continued over line boundaries in free-form source using a special form of statement continuation:

print *, 'I  am a new learner of &
          &fortran'

Usually one often sees free-form statement continuation with simply the & on the non-terminating line as in the question. Within this so-called character context, though, we must have an & on the continuation line and on the continuing line the statement continues with the character immediately following the &, and within the same character context.

For fixed-form source, however, things are different.

      print *, 'I am a new learner of'
     1'fortran'

is like

      print *, 'I am a new learner of'    'fortran'  ! Lots of spaces ignored

which is again an invalid statement. Using

      print *, 'I am a new learner of
     1fortran'

is a valid statement, but again suffers from all those spaces up to column 72.

One could use string concatenation here:

      print *, 'I am a new learner of '//
     1         'fortran'

or simply break the line at column 72 (for after all, we're doing this because of the lines being long).

Solution 3

When continuing the line in question, you need either a comma, which is needed when printing multiple variables or literal constants on one line, or string concatenation //, to concatenate two character strings into one.

This will work:

PRINT *, 'I am a new learner of ', &
     'fortran'

This will work too:

PRINT *, 'I am a new learner of '// &
     'fortran'

Solution 4

In the old code I'm editing the ampersand must appear in the second line after exactly 5 spaces; all other non-comment lines in this code begin with 6 spaces:

      PROGRAM test_ampersand
      IMPLICIT NONE
      PRINT *, 'I am a new learner of'
     & 'fortran'
      END PROGRAM test_ampersand

This has to do with which version of fortran the compiler is expecting, which it may be inferring from the code. Because I am editing legacy code, I have to follow the FORTRAN 77 style. What you describe is the Fortran 95 style.

You can indicate a language version in the filename extension (e.g. .f08 rather than .f) or with compiler options (e.g. -std=f2008). If you are not constrained to an older version, it's probably best to use the most recent version.

(All of the fixed column requirements are related to how early versions of Fortran were encoded on punch cards; newer versions provide other options.)

Share:
12,719
Mahendra Thapa
Author by

Mahendra Thapa

Updated on June 05, 2022

Comments

  • Mahendra Thapa
    Mahendra Thapa almost 2 years

    How does one break a long string and continue on a next line in Fortran?

    I just started using gfortran 4.7.2 in Fedora 17. When I tried to use the following a test code, I am not getting output:

        PROGRAM test_ampersand
        IMPLICIT NONE
        PRINT *, 'I am a new learner of' &
         'fortran'
        END PROGRAM test_ampersand
    

    I was expecting the output as:

    I am a new learner of fortran