Search code examples
formatfortranlinecontinuation

Fortran: FORMAT statement over two lines


I have an old Fortran 77 code, which I would like to run in a F90-Compiler, and my goal is to change the code as less as possible. It works quite well, but I have some problem with format statements in the code. And I don't understand what's the problem. I work with Eclipse, and gfortran. I use free form.

Question 1

This compiles fine:

program HelloWorld
400   FORMAT(7HK GAMMA,2X,G13.5,7H P0,2X,G13.5,6H A1,2X,G13.5) 
end program

This doesn't compile

program HelloWorld
400   FORMAT(7HK 'GAMMA',2X,G13.5,7H 'P0',2X,G13.5,6H 'A1',2X,G13.5)
                                       1
end program

The Error is

P descriptor needs leading scale factor in format string at (1)

(the error is translated from german, might not be exactly the same words in english) What is wrong?

Question 2

This also compiles fine:

program HelloWorld
400   FORMAT(7HK GAMMA,2X,G13.5,7H P0, &
      2X,G13.5,6H A1,2X,G13.5)
end program

If I add more code to the last code:

program HelloWorld
400   FORMAT(7HK GAMMA,2X,G13.5,7H P0,2X,G13.5,6H A1,2X,G13.5, &
      2X,7HK,ALPHA-1,2X,G13.5,7H  BETA-4,2X,G13.5 )
end program

it doesn't compile anymore. The error is:

P Edit descriptor expected in the format string* at (1)

whereas the (1) is placed at the third line after the closing bracket.

*I'm not sure about the translation of "format string", as my console is in german.

What is the problem there?


Solution

  • As I write in the comment it looks more like FORTRAN66 than 77 because the Hollerith H descriptor and data type was used before introducing the CHARACTER data type to the language. It was also used to assign character data to integer variables, but fortunately that is very rare to encounter. The use as an edit descriptor is however more common, although very obsolete.

    It is not clear what you want to achieve, good example of the desired output would be helpful.

    Do you meant:

    400   FORMAT(7HK GAMMA,2X,G13.5,3H P0,2X,G13.5,3H A1,2X,G13.5) 
    

    so that

    print 400, 1. ,2. ,3. 
    

    outputs

    K GAMMA     1.0000     P0     2.0000     A1     3.0000
    

    Or should the P0 and A1 serve as edit descriptors?

    What was the original code in the legacy software?

    The nH Hollerith descriptor just outputs n next characters so it can unintentionally "eat" some of your descriptors and just print them.

    That is the problem that causes that your examples do not compile, because the n before H is too large and the rest of the format then has no sense.

    The next one could be

    400   FORMAT(8H 'GAMMA',2X,G13.5,5H 'P0',2X,G13.5,5H 'A1',2X,G13.5)
    

    to print

     'GAMMA'     1.0000     'P0'     2.0000     'A1'     3.0000
    

    The effect of the above in Fortran 95 and above is better achieved by

    print '(A0,2X,G13.5,A0,2X,G13.5,A0,2X,G13.5)', " 'GAMMA'",1.," 'P0'", 2.0, " 'A1'", 3.0
    

    and maybe you would rather use just:

    print '(A0,2X,G13.5,A0,2X,G13.5,A0,2X,G13.5)', "GAMMA",1.,"P0", 2.0, "A1", 3.0
    

    for printing

    GAMMA     1.0000     P0     2.0000     A1     3.0000
    

    or even

    print *, "GAMMA",1.,"P0", 2.0, "A1", 3.0