Interactive I/O

In the examples shown previously, program output has been performed using write(*,*) statements. The first field inside the brackets (an asterisk in this case) of this write statement is called the output unit. Putting an asterisk in this field indicates that the output should be directed to standard output, i.e. the screen. The second field inside the brackets is the format specifier, which indicates how the resulting output will appear. An asterisk in this field indicates that the output should be written in free format, i.e. with default formatting. Reading input data is performed using the read(*,*) statement, which is analogous to write(*,*). In this section, some of the possible values for the unit and format fields of read and write statements are described.

By providing a format specifier to the write function, the appearance of the resulting output can be controlled. Different format specifiers are used for different data types. Some of the possible format specifiers for integer output are described in the following example.

program formatIntegerOut
implicit none
integer :: i,j
i = 12345678
j = 10

! Print a ruler:
! The 'a' format descriptor indicates that a string will be printed:
write (*,'(a)') " 5 10 15 20 25 30 35"
write (*,'(a)') "----|----|----|----|----|----|----|"

! Field width of zero means that the field expands to fit the output
! The output is left-justified:
write (*,'(i0)') i
write (*,'(i0)') j

! write out two integers with one space between them:
write (*,'(2(i0,1x))') i,j

! Specify a field width of 10: output will be right-justified:
write (*,'(i10)') i

! write 2 integers with field width 10 and 5 spaces between them:
! Output of each will be right-justified:
write (*,'(2(i10,5x))') i,j

! Specify a field width that is too small: Outputs asterisks
write (*,'(i2)') i

! Print another ruler:
write (*,'(a)') "----|----|----|----|----|----|----|"
write (*,'(a)') " 5 10 15 20 25 30 35"
end program formatIntegerOut

Program Output:

    5   10   15   20   25   30   35
----|----|----|----|----|----|----|
12345678
10
12345678 10
  12345678
  12345678            10
**
----|----|----|----|----|----|----|
    5   10   15   20   25   30   35

 

For floating point numbers, a couple of different choices are possible for the output format. These include floating point and exponential notation. Some sample format specifiers for real numbers are given in the following example:

program formatRealOut
implicit none
real :: a,b
a = 21.2345
b = 125.1234

! Print a ruler:
write (*,'(a)') " 5 10 15 20 25 30 35"
write (*,'(a)') "----|----|----|----|----|----|----|"

! 1: Total field width of 10, 4 digits after the decimal point
write (*,'(f10.4)') a

! 2: Expand field width to fit the number, 8 digits after decimal point
write (*,'(f0.4)') a

! 3: 3 digits after decimal point: Note how the output is rounded
write (*,'(f0.3)') a

! Next format some numbers in exponential notation
! 4: e format specifier gives a number between 0.1 and 1 times a power of 10
! This example prints a number with total field width 10
write (*,'(e10.4)') a

! 5: es format specifier gives a number between 1.0 and 10.0 times a power of 10
! This example prints two values separated by 5 spaces:
write (*,'(2(es10.4,5x))') a,b

! 6: field width too small - prints asterisks
write (*,'(es5.4)') a

! Print another ruler:
write (*,'(a)') "----|----|----|----|----|----|----|"
write (*,'(a)') " 5 10 15 20 25 30 35"
end program formatRealOut

Program Output:

    5   10   15   20   25   30   35
----|----|----|----|----|----|----|
   21.2345
21.2345
21.235
0.2123E+02
2.1235E+01     1.2512E+02
*****
----|----|----|----|----|----|----|
    5   10   15   20   25   30   35

Supported By

File Browser Reference
Department FHERIS
University of Galway
HEA Logo