Programs

Self-Printing Fortran Program

      REAL*8F(17)
      DATA F/8H(7X,11HR,8HEAL*8F(1,8H7)/7X,7H,8HDATA F/,,8H5(2H8H,A,
     18H8,1H,)/6,8HX,1H1,6(,8H2H8H,A8,,8H1H,)/6X,,8H1H2,5(2H,8H8H,A8,1H,
     28H,),2H8H,,8HA8,1H//7,8HX,11HWRI,8HTE(6,F)F,8H/7X,3HEN,8HD)      /
      WRITE(6,F)F
      END

The program declares an array of 8 byte real numbers "F" 17 elements long and then initializes them using a 3-line DATA statement. I have highlighted the initialization values in orange above. The variable F essentially has the string value:

(7X,11HREAL*8F(17)/7X,7HDATA F/,5(2H8H,A8,1H,)/6X,1H1,6(2H8H,A8,1H,)/6X,1H2,5(2H8H,A8,1H,),2H8H,A8,1H//7X,11HWRITE(6,F)F/7X,3HEND)

The program then very craftily uses the array itself as the format statement to output the array.

FORTRAN neophytes, note that:

- The first 5 characters of a statement are line numbers, which are not used at all in this program. Column 6 is a continuation indicator. Any non-blank value (traditionally an ascending sequence such as the 1 and 2 used in this program) means that the remainder of the line is a continuation of the previous statement.
- a Hollerith constant is of the form nHabc.., that is, a number followed immediately by an upper-case H, followed by the text string. for example, the first one that occurs in this program is 8H(7X,11HR. It is equivalent to what would be expressed in other computer languages as "(7X,11HR". All 17 values of the REAL array are each initialized to 8 bytes of alphanumeric values.
- nX in a FORMAT statement causes n spaces to be output.
- / in a FORMAT statement causes a new line.
- A8 is a specifier to output a value as 8 alphanumeric characters.
- The output processor always strips the first column of output away as "carriage-control". A blank in this position just means to output the line normally.

Programs