Formatted Input Output
Formatted Input Output
We have so far seen that we can read data from keyboard using the read * statement,
and display output to the screen using the print* statement, respectively. This form of
input-output is free format I/O, and it is called list-directed input-output.
However the formatted I/O gives you more flexibility over data transfer.
Where,
Format specification defines the way in which formatted data is displayed. It consists of a
string, containing a list of edit descriptors in parentheses.
An edit descriptor specifies the exact format, for example, width, digits after decimal
point etc., in which characters and numbers are displayed.
For example
Print "(f6.3)", pi
https://www.tutorialspoint.com/fortran/fortran_basic_input_output.htm 1/6
11/16/2018 Fortran Basic Input Output
ES This is used for real output (scientific notation). This print "
(es10.3)",123456.0
takes the form ‘rESw.d’ where the meanings of r, w
gives ‘1.235e+05’
and d are given in the table below. The ‘E’ descriptor
described above differs slightly from the traditional
well known ‘scientific notation’. Scientific notation has
the mantissa in the range 1.0 to 10.0 unlike the E
descriptor which has the mantissa in the range 0.1 to
1.0. Real values are right justified in their fields. If the
field width is not large enough to accommodate the
real number then the field is filled with asterisks. Here
https://www.tutorialspoint.com/fortran/fortran_basic_input_output.htm 2/6
11/16/2018 Fortran Basic Input Output
This is used for space output. This takes the form ‘nX’ print "(5x, a10)",
X
where ‘n’ is the number of desired spaces. str
c
1
Column number
d
2
Number of digits to right of the decimal place for real input or output
m
3
Minimum number of digits to be displayed
n
4
Number of spaces to skip
r
5
Repeat count – the number of times to use a descriptor or group of descriptors
6 w
Field width – the number of characters to use for the input or output
https://www.tutorialspoint.com/fortran/fortran_basic_input_output.htm 3/6
11/16/2018 Fortran Basic Input Output
Example 1
Live Demo
program printPi
pi = 3.141592653589793238
Print "(f6.3)", pi
Print "(f10.7)", pi
Print "(f20.15)", pi
Print "(e16.4)", pi/100
When the above code is compiled and executed, it produces the following result −
3.142
3.1415927
3.141592741012573
0.3142E-01
Example 2
program printName
implicit none
read *,first_name
print "(1x,a)",first_name
When the above code is compiled and executed, it produces the following result: (assume
the user enters the name Zara)
Example 3
Live Demo
program formattedPrint
implicit none
print "(i6)", k
https://www.tutorialspoint.com/fortran/fortran_basic_input_output.htm 4/6
11/16/2018 Fortran Basic Input Output
print "(i6.3)", k
print "(3i10)", n, k, i
print "(i10,i3,i5)", n, k, i
print "(a15)",str
print "(f12.3)", d
print "(e12.4)", c
print '(/,3x,"n = ",i6, 3x, "d = ",f7.4)', n, d
When the above code is compiled and executed, it produces the following result −
45
045
300789 45 2
300789 45 2
Tutorials Point
123.457
0.1279E-08
n = 300789 d = *******
print 100
100 format (7x,'Name:', 7x, 'Id:', 1x, 'Weight:')
When the above code is compiled and executed, it produces the following result −
https://www.tutorialspoint.com/fortran/fortran_basic_input_output.htm 5/6