Fortran Modern alternatives to historical features Assigned format specifiers

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Before Fortran 95 it was possible to use assigned formats for input or output. Consider

integer i, fmt
read *, i

assign 100 to fmt
if (i<100000) assign 200 to fmt

print fmt, i

100 format ("This is a big number", I10)
200 format ("This is a small number", I6)

end

The assign statement assigns a statement label to an integer variable. This integer variable is later used as the format specifier in the print statement.

Such format specifier assignment was deleted in Fortran 95. Instead, more modern code can use some other form of execution flow control

integer i
read *, i

if (i<100000) then
  print 100, i
else
  print 200, i
end if

100 format ("This is a big number", I10)
200 format ("This is a small number", I6)

end

or a character variable may be used as the format specifier

character(29), target :: big_fmt='("This is a big number", I10)'
character(30), target :: small_fmt='("This is a small number", I6)'
character(:), pointer :: fmt

integer i
read *, i

fmt=>big_fmt
if (i<100000) fmt=>small_fmt

print fmt, i

end


Got any Fortran Question?