ENG 213.1 · Computer Programming for Engineers
Our notes
1 min readList-directed input and output
The simplest form uses * for "the default format":
read *, a, b, c
print *, 'Sum =', a + b + c
The user types the values separated by spaces or commas and presses Enter.
Prompting the user
print *, 'Enter pipe diameter (m) and velocity (m/s):'
read *, d, v
Always say what to type and in which units.
Formatted output
List-directed output prints many digits in a layout the compiler chooses. For neat tables, give a format:
print '(A, F8.3)', 'Velocity (m/s) = ', v
| Descriptor | Meaning | Example output |
|---|---|---|
| Iw | integer in w columns | I5 → " 42" |
| Fw.d | real, w columns, d decimals | F8.3 → " 12.346" |
| Ew.d | exponent form | E12.4 → " 0.1235E+02" |
| ESw.d | scientific (one digit before the point) | ES12.4 → " 1.2346E+01" |
| A | text | the string as is |
| nX | n spaces | 3X |
| / | new line |
If the number does not fit in w columns, Fortran prints asterisks: always allow room for the sign and the decimal point.
Worked example: a table
program ctof
implicit none
integer :: c
real :: f
print '(A)', ' C F'
do c = 0, 100, 25
f = 9.0 / 5.0 * c + 32.0
print '(I4, F8.1)', c, f
end do
end program ctof
Output:
C F
0 32.0
25 77.0
50 122.0
75 167.0
100 212.0
Using the FORMAT statement (older style)
write(6, 100) x, y
100 format(1X, 'X =', F8.3, ' Y =', F8.3)
Unit 6 is normally the screen and unit 5 the keyboard. Modern code usually puts the format string in the statement itself.
Exams usually ask
- Write READ and PRINT statements for given data.
- Show the exact output of a formatted PRINT statement.
Common mistake
Choosing a field too narrow: printing 1234.567 with F6.2 gives because the value needs at least 7 columns.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Input/output
(opens en.wikipedia.org in a new tab)
Wikipedia, Input/output, CC BY-SA 4.0 · licence
-
printf
(opens en.wikipedia.org in a new tab)
Wikipedia, printf, CC BY-SA 4.0 · licence
Watch
-
Best pick Ep 13: Fortran Computes, Python Plots | File Input and Output
16 min · ≈ 72 MB · about ₦40
(opens youtube.com in a new tab)
-
FORTRAN Formatted Input Output
18 min · ≈ 81 MB · about ₦45
(opens youtube.com in a new tab)
-
FORTRAN: READ AND PRINT STATEMENT
28 min · ≈ 126 MB · about ₦71
(opens youtube.com in a new tab)
-
FORTRAN 05: Keyboard Input and sharing I/O Formats using the format Statement
40 min · ≈ 180 MB · about ₦101
(opens youtube.com in a new tab)
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.