Line continuation of strings in Fortran

fortran 90 line length limit
fortran continue
how to comment multiple lines in fortran
fortran 77
fortran label field of continuation line is not blank
fortran asterisk
break in fortran
fortran 77 main

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

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.

Line continuation of strings in Fortran, A continuation line in free-form Fortran is created with an ampersand as Lines can be broken within a string constant, in which case the  Copyrights. A continuation line in free-form Fortran is created with an ampersand as follows: integer, dimension(5) :: foo = (/ 1, 2, & 3, 4, 5 /) There is also an optional ampersand at the beginning of the continuation line. For example:

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'

Continuation lines in Fortran Wiki, Fortran Continuation Lines. In Fortran, a statement must start on a new line. If a statement is too long to fit on a line, it can be continued with the following  Fortran Continuation Lines. In Fortran, a statement must start on a new line. If a statement is too long to fit on a line, it can be continued with thefollowing methods: If a line is ended with an ampersand, &, it will be continued on the next line. Continuation is normally to the first character of the next non-comment line.

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. If we do that, though, all the leading blanks in the line become part of the character literal. With the & on the continuing line, the statement continues with the immediately following character, not the first on the line.

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).

Fortran continuation lines, . It is an executable statement but it takes no action in the program. It's primarily used as a place holder to span gaps created between branches or loops and the rest of the program. The Fortran language can treat characters as single character or contiguous strings. A character string may be only one character in length, or it could even be of zero length. In Fortran, character constants are given between a pair of double or single quotes.

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.)

Fortran 77 Tutorial, A continuation character is any character of the GNU Fortran character set other than space (<SPC>) or zero (` 0 ') in column 6, or a digit (` 0 ' through ` 9 ') in  Continuation lines are identified by an ampersand (&) in column 1, or a nonzero digit after the first tab. Mixing Formats . You can format lines both ways in one program unit, but not in the same line. Continuation Lines . The default maximum number of continuation lines is 99 @ (1 initial and 99 continuation).

Statement Purpose, Continuation lines are indicated by an ampersand ( & ) after the code at the end of and had a text line of (say) 170 characters, the & on the wrapped line Compiling with Intel® Visual Fortran Compiler XE 12.1.5.344 [IA-32]. So, str1 is processed with the first format item A, and a string appears. Come the second output item str2 we've already used the single format item, reaching the end of the format item list. The result is that we see this format reversion: that is, we go back to the first item in the list. After, crucially, we start a new line.

PARAMETER (FORTRAN 77 Language Reference), line continuation character Fortran 90 Programming with NR. Note that enclose the strings with<> brackets when I print them like this so that I  Count_Items in string that are blank or comma separated! Reduce_Blanks in string to 1 blank between items, last char not blank! Replace_Text in all occurances in string with replacement string! Spack pack string's chars == extract string's chars! Tally occurances in string of text arg!

Continuation Line, Continuation lines are identified by a nonblank, nonzero in column 6. Short lines are padded to 72 characters. Long lines are truncated. See "Extended Lines". So the concatenated string would be 'A' + 259 blanks + 'B' + 259 blanks, in total 520 characters. Since zz is only 260 characters long, the rest is cropped. What you are trying to do is achieved by. zz = trim(xx) // trim(yy) trim() removes trailing whitespace from strings.

Comments
  • Most of the times, it is also useful to include compiler error message if the program does compile, or a run-time error message if the program compiles but does not run.
  • When I used gfortran, following message appeared: PRINT *, 'I am a new learner of & 1 Error: Unterminated character constant beginning at (1) Ampersand.f:8.10: &fortran' 1 Error: Invalid character in name at (1)
  • When I followed above suggestions using gfortran, error message was as: PRINT *, 'I am a new learner of ', & 1 Error: Expected expression in PRINT statement at (1) PRINT *, 'I am a new learner of '// & 1 Error: Syntax error in expression at (1) Ampersand.f:6.9: 'fortran' 1 Error: Invalid character in name at (1)
  • -std=f2008 cannot help. This is not controlled by the standard version but by the source form (fixed form vs. free form). It is perfectly legal and possible to have Fortran 2008 source in fixed form. The suffix does indicate the form, but some compilers do not accept .f08, but only .f90. The .f90 does not mean Fortran 90, it means free form fortran source. It is confusing, but it is how it works.