Question

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?

Was it helpful?

Solution 2

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

OTHER TIPS

Your format statements have an H (for Hollerith) edit descriptors - the things in the format statements that have a number followed immediate by a H. The descriptor results in the characters (including blanks) following the H and counted by the leading number being output to the file.

This language feature was made obsolescent in Fortran 90 and removed completely from the language in Fortran 95.

They are very error prone. Since Fortran 77 a far better way of achieving the same result has been to use character constant edit descriptors.

The problem is that you have (or are creating) a mismatch between the number of characters indicated by the leading number and the actual count of characters that apparently were in the descriptor. For example, your second FORMAT statement is copied below, showing the seven characters that would be in the descriptor. You can see how that appears to end in the middle of a "string". This then confuses what the compiler sees for the remainder of the format specification.

400   FORMAT(7HK 'GAMMA',2X,G13.5,7H 'P0',2X,G13.5,6H 'A1',2X,G13.5)
               1234567
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top