Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line continuation of strings in Fortran

Tags:

string

fortran

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
like image 581
Mahendra Thapa Avatar asked May 27 '13 15:05

Mahendra Thapa


2 Answers

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.

like image 51
High Performance Mark Avatar answered Oct 20 '22 16:10

High Performance Mark


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

like image 38
francescalus Avatar answered Oct 20 '22 15:10

francescalus