An input statement reads the information from an input device or a file. It then separates the info into separate items converting them into binary form understood to computer. Finally, each item resides in a separate variable allocated to it for further processing. On the other hand, output statements read the items from the processed variables and convert them into a form suitable for output. The separate items holding the information then combine to a suitable meaningful form for final output.
READ and PRINT are the two most common I/O commands in FORTRAN 77. Writing these commands in a specific format constitutes an I/O statement in the program. Mainly two types of input output statements are there in FORTRAN77 : List-directed and Format commands
READ * , Variable_list PRINT * , Expression_list
In List- Directed statements, an asterisk (*) symbol follows the READ or PRINT statements. Variable_list and expression_list consists of items separated by commas. Asterisk symbol ensures that FORTRAN reads according to the data type of the items in the variable_list and write according to the data types of the items in the expression_list. List_Directed statements are most suitable when entering numerical data from the keyboard. However, it is problematic when entering character data. Because character data has to be typed in the quotes everytime.
Formatted I/O statements
Format statements help to achieve greater control over the conversion and formatting of Input Output statements. An extra FORMAT statement replaces the asterisk (*) symbol in formatted I/O statements.
READ Variable_list label FORMAT (specification_list)
PRINT Variable_list label FORMAT (specification_list)
FORMAT statement decides the layout of the each item in the variable_list and also its conversion between internal & external forms. A label always precedes the FORMAT statement providing a reference for its use in I/O statements. Placing the FORMAT statements at the end of the program makes multiple use of the statement easier. Specification_list is a list of format descriptors with commas in between. There are many format descriptors that are employed both in input and output statements.
Input Format Descriptors
I descriptor:- General form of I descriptor is rIw, where r is repetition indicator, I is the descriptor and w is the width of the field. Width of the field determines the number of positions (characters) in the read value. Thus an I descriptor converts w characters to an INTEGER value. These characters include decimal digits and/or spaces with an optional + or – sign. To understand, consider the following commands:
FORMAT statement has label 10. So, to read the variables using this format statement, label 10 is put in READ statement. Descriptor I5 makes the program read initial 5 characters of the input and report them as an integer. For example when the input is 345454566 (as given in command prompt) , this program gives the output 34545.
F descriptor:- This descriptor reads a value in REAL variable. General form of F descriptor is Fw.d, where w is an unsigned integer representing the width of field. d represents the number of digits in the fractional part i.e. w-d digits are in the part before decimal.
So, for descriptor F6.5, if the input is 2344537.4388 then this program reads it as 2.34453. Here, total number of digits is 6 with 5 digits in decimal part.
E-descriptor:- This descriptor reads a value in REAL variable, but in exponential notation. General form is Ew.d. w is the depth of field i.e. total number of characters in the variable with fractional part rounded to d number of decimal places.
X-descriptor:– General form of this descriptor is nX.
T, TL and TR-descriptor:- T stands for tab and moves the cursor to a particular position. TR shifts the cursor to right, while TL to the left. In the example below, for the input 734324637432, program reads it as 32.
Output Format Descriptors
Output statements use same descriptors as for input ones. Computer uses the output list and corresponding format specifications to build the lines of output. These lines are stored in a storage unit called output buffer before actual display. First character of each line in the output buffer does not appear on the display. So in order to avoid any mistake in the results, some control characters are placed as the first character in the FORMAT command itself.
In addition to these, a literal format descriptor also populates the box of output descriptors. It is a string of characters for output. There are 2 literal format descriptors:- ‘c1c2c3…cn’ and nHc1c2…cn. These place the string of n characters directly into the output buffer.