Internal I/O writes formatted output into a character variable instead of a file or screen.

Program

Play the program to format a number into a string buffer.

internal_io.f90
Replay: real traced execution (multi-file project)
program internal_io
    implicit none
    character(len=32) :: buf
    integer :: count
    count = 42
    write(buf, '(A, I0)') "count=", count
    print '(A)', trim(buf)
end program internal_io
  1. count ← 42

    4integer :: count5count = 426write(buf, '(A, I0)') "count=", count
    values this step42count
  2. buf ← count=42

    5count = 426write(buf, '(A, I0)') "count=", count7print '(A)', trim(buf)
    values this stepcount=42buf42count
  3. print '(A)', trim(buf)

    6    write(buf, '(A, I0)') "count=", count7    print '(A)', trim(buf)8end program internal_io
    outputcount=42
    values this stepcount=42buf

Follow the Buffer

  1. count starts as 42.
  2. write(buf, '(A, I0)') "count=", count writes into buf.
  3. The buffer text becomes count=42.
  4. trim(buf) removes unused blanks before printing.
  5. The program prints count=42. | step | buffer/output | | --- | --- | | number | 42 | | after internal write | count=42 | | printed text | count=42 |
internal write `write(buf, fmt) ...` writes into a character variable.
format `'(A, I0)'` joins a string and a minimum-width integer.
trim `trim(buf)` strips trailing blanks before printing.

Exercise: internal_io.f90

Reproduce count=42, then trace which line writes the number into the buffer before it is printed.