4. Format

The old Fortran programs used numbering of the format statements. However, that doesn't look very good in pure Fortran 90, where you don't use statement numbers except in a few exceptional cases. In Fortran 77 there already was a facility, which however was not used very much, to use a format variable (of type CHARACTER ) instead of a numbered format. The Format variable was put directly in the input/output statement. Now we will show three different ways of doing this assignment. They both have their advantages and disadvantages. The program follows
     PROGRAM FORMAT
     IMPLICIT NONE
     REAL                         :: X
     CHARACTER (LEN=11)           :: FORM1
     CHARACTER (LEN=*), PARAMETER :: FORM2 = "( F12.3,A )"
     FORM1 = "( F12.3,A )"
     X = 12.0
     PRINT FORM1, X, ' HELLO '
     WRITE (*, FORM2) 2*X, ' HI '
     WRITE (*, "(F12.3,A )") 3*X, ' HI HI '
     END
In the PRINT statement we use the character string variable FORM1 with the length 11, which is assigned its value in an explicit assignment statement. The difficulty with this method is essentially that you have to manually count the number of characters, if it is too small the NAG compiler will not give a compilation error, but the error will show up at execution.

In the first WRITE statement we use a character string constant FORM2 instead. The advantage is that with the PARAMETER statement it is not necessary to give an explicit length of the constant, but it can be given the length with the statement LEN=*. The bad thing is that we can not assign the constant a new value.

In the second WRITE statement we use an explicit character string directly. The difficulty is that the string can not be reused.

Exercises

(4.1) What does this statement give as its output?
     WRITE(*, "( HI )") 
Solution.

(4.2) What does the following statement perform?

     CHARACTER (LEN=9)      :: FILIP
     FILIP = '(1PG14.6)'
     WRITE(*,FILIP) 0.001, 1.0, 1000000.
Solution.


Last modified: 6 April 1999
boein@nsc.liu.se