Skip to main content

Formatted and unformatted I/O in FORTRAN

Short answer

Formatted I/O uses a FORMAT statement with edit descriptors (e.g., 2I4) to control output; example: WRITE(*,20) L,B. Unformatted I/O uses READ * (list‑directed) or binary file I/O without a format.

What do you mean by formatted and unformatted input/output statements in Fortran and also give suitable example which explain the concept of Formatted I/O?

Formatted input/output

In formatted I/O the programmer controls the exact form of the data by a FORMAT statement (edit descriptors) referred to by a label in READ or WRITE. Data are converted between their internal form and readable text.

Syntax: WRITE(unit, label) list and label FORMAT(descriptors)

Common descriptors: Iw integer, Fw.d real with d decimals, Ew.d exponent form, Aw character, nX n blanks, / new line, and quoted text.

Unformatted input/output

  • List-directed (free format): READ *, A, B and PRINT *, A, B (or READ(*,*)) do not use a FORMAT. The computer decides the layout, with values separated by blanks or commas. Many textbooks call this unformatted I/O.
  • In the strict sense, unformatted I/O means writing and reading the internal binary form to a file (OPEN(..., FORM='UNFORMATTED'), then WRITE(10) list). It is fast, with no conversion, but not readable by people.

Example of formatted I/O

      PROGRAM RECT
      INTEGER L, B
      REAL A
      READ(*,10) L, B
      A = L * B
      WRITE(*,20) L, B, A
   10 FORMAT(2I4)
   20 FORMAT(' LENGTH =', I4, '  BREADTH =', I4, /, ' AREA =', F8.2)
      STOP
      END

For input 12 5 the output is

 LENGTH =  12  BREADTH =   5
 AREA =   60.00

2I4 reads two integers of width 4, F8.2 prints a real in 8 columns with 2 decimals, and / starts a new line.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 11 questions →