Edit Descriptors and the PRINT/WRITE Statement
Fortran's formatted I/O uses edit descriptors packed inside a format string to control exactly how each value is rendered: I5 prints an integer right-justified in a 5-character field, F8.2 prints a real number in fixed-point notation with 8 total characters and 2 decimal places, and A10 prints a character string in a 10-character field. A statement like WRITE(*, '(A, I5, F8.2)') 'Result: ', count, total applies each descriptor to the corresponding output item in order, giving column-aligned output that list-directed WRITE(*,*) cannot guarantee.
Cricket analogy: A scoreboard that always prints the batsman's score in a fixed 3-digit field so single, double, and triple-digit scores all align in a column mirrors I3 forcing consistent field width regardless of the value's size.
Common Numeric Descriptors
Beyond I and F, Fortran offers E for scientific notation (E12.4 gives something like 0.1234E+05), ES for scientific notation with the mantissa normalized to a single leading nonzero digit, G as a general descriptor that picks a sensible representation automatically, and X for inserting blank spaces between fields, as in WRITE(*, '(I3, 2X, F6.2)'). Repeat counts and grouping are also supported: 3(I4, 1X) repeats that pair of descriptors three times, and the slash / descriptor forces output to continue on a new line within a single WRITE statement.
Cricket analogy: Printing a bowling figure like 4-32 with two spaces of padding before the economy rate, using I2,2X,F5.2, mirrors the X descriptor inserting deliberate blank space between output fields.
Reading Formatted Input
The same edit descriptors that control output also control input parsing with READ, so READ(unit, '(I4, F6.2)') code, price expects exactly four columns for an integer followed by six columns for a fixed-point real, regardless of embedded spaces. This fixed-column reading is essential for parsing legacy data files where fields are positioned by column rather than delimited by commas or spaces, but it means a single misaligned column in the input file silently reads garbage or triggers a runtime format error rather than gracefully skipping whitespace the way list-directed READ(*,*) would.
Cricket analogy: Reading a paper scoresheet where the over number always occupies columns 1-3 and the runs always occupy columns 4-6 mirrors fixed-column formatted READ expecting data at exact character positions.
Reusable FORMAT Statements
Instead of inlining a format string in every WRITE, a labeled FORMAT statement can be defined once and referenced by its label: 100 FORMAT(A10, I5, F8.2) followed elsewhere by WRITE(*, 100) name, id, salary. Modern style favors character-parameter format strings instead of numeric labels, such as CHARACTER(LEN=*), PARAMETER :: FMT1 = '(A10, I5, F8.2)' and WRITE(*, FMT1) ..., which avoids scattering unlabeled magic numbers through a program and lets one format definition be reused consistently across many WRITE statements that must line up in the same table.
Cricket analogy: Defining one reusable scoreboard template used for every innings display, rather than redesigning the layout each time, mirrors a labeled FORMAT statement reused across multiple WRITE statements.
program formatted_io_demo
implicit none
character(len=*), parameter :: hdr_fmt = '(A10, A8, A10)'
character(len=*), parameter :: row_fmt = '(A10, I8, F10.2)'
character(len=10) :: name
integer :: id
real :: salary
write(*, hdr_fmt) 'Name', 'ID', 'Salary'
name = 'Alice'
id = 1042
salary = 68500.5
write(*, row_fmt) name, id, salary
name = 'Bob'
id = 1108
salary = 5321.25
write(*, row_fmt) name, id, salary
! Reading fixed-column data: columns 1-4 integer, 5-10 real
block
integer :: code
real :: price
character(len=10) :: line = '1042 68.50'
read(line, '(I4, 1X, F5.2)') code, price
print *, 'parsed code =', code, ' price =', price
end block
end program formatted_io_demoIf a numeric value is too large to fit its field width (for example printing 123456 with an I5 descriptor), Fortran does not silently truncate -- it fills the entire field with asterisks (*****) to signal an overflow. Always size numeric fields generously enough for the largest expected value.
- Edit descriptors (I, F, E, ES, A, X) precisely control field width and representation for each output item.
- F8.2 means a fixed-point field 8 characters wide with 2 digits after the decimal point.
- Repeat counts like 3(I4,1X) and the slash (/) descriptor control repetition and line breaks within one WRITE.
- READ with a format string parses input by fixed column position, unlike flexible list-directed READ(*,*).
- Reusable FORMAT labels or character-parameter format strings avoid duplicating format definitions.
- Overflowing a numeric field produces a row of asterisks rather than silent truncation or wraparound.
- Formatted I/O is essential for producing aligned tables and parsing fixed-width legacy data files.
Practice what you learned
1. What does the edit descriptor F8.2 specify?
2. What happens if a value is too large to fit in its specified field width?
3. How does formatted READ differ from list-directed READ(*,*)?
4. What does the X edit descriptor do?
5. Why prefer a character-parameter format string over a numeric FORMAT label in modern code?
Was this page helpful?
You May Also Like
File Handling in Fortran
Opening, reading, writing, and closing files in Fortran using OPEN/CLOSE, unit numbers, and formatted, list-directed, and unformatted access modes.
Intrinsic Functions
The built-in numeric, array, and character functions Fortran provides out of the box, from SQRT and MOD to SUM, MAXVAL, and TRIM.
Array Operations and Slicing
Whole-array arithmetic, array sections, and the WHERE/FORALL constructs that let Fortran express vectorized operations without explicit loops.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics