next up previous contents
Next: Summary Up: FORMAT statements Previous: FORMAT statements

   
General Discussion

FORMAT statements allow the user greater control of the layout of data during I/O than is possible with the format reference * introduced in section 4. Every FORMAT statement has a label, and it is its label that is used instead of * to invoke its use. The use of the FORMAT statement is demonstrated below :-

        NUM=123
        REAL=123.456
        PRINT 90000,NUM,NUM,REAL,REAL
90000   FORMAT(' ','NUM = ',I7,I7.5,' REAL = ',F10.1,E15.5)
        STOP
        END

The PRINT statement uses the FORMAT statement 90000 which happens to be the next line - although the FORMAT statements can be placed anywhere and some people put all the FORMAT statements together at the end of the program. If the above program is run it would produce the following output:-

 
    NUM =     123  00123 REAL =123.5     0.12346E 3

The FORMAT statement consists of a list of FIELD DESCRIPTORS separated by commas and enclosed in brackets. The first descriptor is a single character space ' '. This is called the CARRIAGE CONTROL character and is used to determine how many lines are to be skipped before printing the next line. The most commonly used characters are:-

 
    ' '   start new line
    '0'   miss one line and start next line
    '1'   start next page (useful when printing on a line printer)
    '+'   remain on current line

When producing output on the teletype or on a line printer the first character is always used as a carriage control and is not printed. If the user has forgotten to include one then the first character of his or her data will be used instead. When producing output to a file there is no special action with the first character (i.e. it is stored) but if the file is subsequently output on a terminal or a printer then at that stage the first character will be used as carriage control. Next in the FORMAT statement comes the character string 'NUM = ' and this is output just as it stands. The carriage control character was shown as a separate string for clarity but the two strings could have been combined into the one string ' NUM = ' with exactly the same result. Now comes I7 which means that the next element (in this case the first) in the I/O list of the PRINT statement must be output as an integer occupying 7 character positions. FORTRAN by default, replaces leading zeros by blanks (except that 0 is represented by one 0!) and outputs the number right justified, so that the output of the variable INUM contains 4 spaces and then 123. The I7.5 has a similar effect on the next element in the I/O list except that the .5 means that the number must have at least 5 non blank characters even if this means having leading zeros, so 2 spaces and then 00123 is produced. The string ' REAL = ' comes next and is output as it stands. A point to note is that as numbers are right justified it is usually a good idea to start a character string with at least one space after printing a number so as to separate them. The F10.1 directs that the next element on the list (which is the third element REAL) is to be printed as a floating point number (i.e. a real) occupying 10 character positions with exactly 1 after the decimal point. Unlike the I field descriptor the second number after the decimal point in the F field descriptor is not optional. Note that the number has been correctly rounded up because the descriptor was not sufficient to display to full accuracy (it would not have been necessary if F10.3 had been used) The last element of the FORMAT statement is E15.5 which takes the next element in the I/O list (which also happens to be the last) and displays it as a floating point number using 15 character positions and 5 after the decimal point in a form that shows the exponent as well. By default FORTRAN arranges for the number to be between .1 and 1. and adjusts the exponent accordingly.

Exercise 11

Write a simple program to output integers and reals using a FORMAT statement. Find out what happens if :-

1.
The number needs more characters than is allowed (which must include a - sign for negative numbers). e.g. what happens if NUM=-55 and is displayed using I2?
2.
The number of field discriptors in the FORMAT statement - not counting character strings is not equal to the number of elements in I/O list.
3.
Integers are displayed as if real e.g. NUM as F7.2 and reals as if integer e.g. REAL as I9

* * *

So far we have only dealt with output, now we shall consider input with the following program :-

        PRINT *,'INPUT A AND I'
        READ 123,A,I
        PRINT *,'A = ',A,' I= ',I
        STOP
123     FORMAT(F5.3,I2)
        END
If this program is run and the number :-
123456789

typed in reply to the request to input A and I, then A would contain 12.345 and I would have 67. As before, the FORMAT statement consists of a list of field descriptors and these are used one by one as the I/O list is processed. Unlike FORMATs used for input there is no carriage control, or indeed any character string. In the above example the F5.3 takes the first 5 characters and inserts a decimal point so that there are 3 characters behind it. This number is stored in A while I receives an integer using I2 which consists of the next two characters. The remaining data on the input line is ignored. It is not normal practice to input real numbers without supplying a decimal point, and if one is used, then it overrides the number given in the F field descriptor e.g. if the above program were run and the number

-12.340
were typed in then A would be set to -12.3 and I to 40. So the width of the field descriptor F5.3 (i.e. how many characters wide) is used but the number after the decimal point is ignored. Also note that if the record typed in is too short it is extended with spaces and that spaces are treated as zeros so that I2 processes 4 space and converts it to 40. Fixed width field descriptors are fine when inputing data from a file but easily lead to errors when reading from the terminal. Imagine trying to type in dater using e.g.
7 FORMAT(F5.0,I2,I7,F9.0,F3.0,I1,I5)
miscount by one character or fail to right justify all integers and wrong numbers would result. This problem is overcome by using variable width descriptors which have no field width or number after the decimal point. So the above statement would be written:-
7 FORMAT(F,I,I,F,F,I,I)
When typing data in the user must include the decimal point on real numbers and end each number with a comma. So, for example:-
3.14159, -9, 123,.2,.2E-5, 99, 0,
would be legal data for the above FORMAT statement. Note also that real numbers with an exponent can be input using an F descriptor. In fact E is equivalant to F for input - either will accept a number with or without an exponent.
Exercise 12
Write a simple program to read reals and integers from the terminal using variable width descriptors. What happens if:-

1.
The decimal point is omitted when inputing a real (i.e. it is presented as an integer)
2.
A real number is typed in when inputting an integer
3.
Spaces but no commas are used to separate the numbers.

* * *

Field descriptors can be preceeded by a repeat count e.g. 3F7.2 is the same as F7.2,F7.2,F7.2. Brackets can be used with a repeat count to repeat a group of descriptors e.g. 2(I5,F9.1) is the same as I5,F9.1,I5,F9.1 FORMAT statements may include the end of record character / which terminates the current record and starts the next one. For example:-

        PRINT 90001
90001   FORMAT (' THIS IS LINE l',/,'0 AND THIS IS LINE 3')
        STOP
        END

The first character (a space) of ' THIS IS LINE 1' is used as a carriage control and the rest of the string output. The / starts a new record (i.e. a new line) which requires a new carriage control which in this case is 0 which skips one line and then outputs 'AND THIS IS LINE 3'. Note also this shows an output statement (PRINT) without an I/O list. The / can also be used on input. To read 2 numbers using:-

99 FORMAT (F,/,F)
would require the first number to be input on one line and the second number to input on the next line. By using multiple /s records may be skipped e.g. /// skips 2 records. The / may appear at the start of the FORMAT statement e.g.
7000 FORMAT (///,' HELLO')
would skip 3 lines before outputing 'HELLO' the /// skips 2 records and then the carriage control ' ' start at the next record skipping 3 lines altogether. The character $ (dollar) may be included at the end of an output FORMAT statement to suppress the carriage return and is used to improve readability of a question and answer dialogue between the computer and the user. e.g.

 
        PRINT 90000
 
        READ 90001,IEVENT
 
        .
        .
        .
 
90000   FORMAT(' Event number = ',$)
90001   FORMAT(I)

after typing 'Event number = ' the cursor does not return to the left hand side so if the user now types 123 and presses RETURN, then the dialogue appears as:-

Event number = 123

The $ is NOT standard FORTRAN; it only works on the VMS.


next up previous contents
Next: Summary Up: FORMAT statements Previous: FORMAT statements
n west (APC)
2000-03-08