The Fortran 2003 standard constitutes a strong foundation of “modern Fortran”. Modern Fortran (Fortran ≥ 2003) is so different in capabilities and coding style from Fortran 77 as to be a distinct, highly backward compatible language. Of course, almost all of Fortran 95 was incorporated into Fortran 2003, except for a few obscure little used and confusing features deprecated and already unsupported by some popular compilers.
Writing to console effectively
The use of
write(*,*) grew out of non-standard use of Fortran 66’s
write statement that was introduced for device-independent sequential I/O.
write(*,*) became part of Fortran 77 for printing to console standard output, the Fortran 77
write command is not needed,
write is needed more distinct.
Assembly language comparison
print *,'hi' and
write(*,*) 'hi' are IDENTICAL in assembly, within each of multiple compilers (gfortran, flang, ifort) as it should be.
In general, disassemble Fortran executables with:
gfortran myprog.f90 objdump --disassemble a.out > myprog.s
Fortran 2003 finally settled the five-decade old ambiguity over console I/O with the intrinsic
iso_fortran_env module, which is often invoked at the top of a Fortran module like:
module mymod use, intrinsic:: iso_fortran_env, only: stdout=>output_unit, stdin=>input_unit, stderr=>error_unit
=> operators are here for renaming (they have other meanings for other Fortran statements).
It’s not necessary to rename, but it’s convenient for the popularly used names for these console facilities.
- For routine console printing, use:
print *,'my text'
- For advanced console printing, whether to output errors, use non-advancing text, or toggle between log files and printing to console, use
write(stdout,*)or the like.
Print to stdout console if output filename not specified
program file_or_console use, intrinsic:: iso_fortran_env, only: stdout=>output_unit implicit none character(:), allocatable :: filename character(256) :: buf integer :: i, u call get_command_argument(1,buf,status=i) if (i==0) then filename = trim(buf) ! Fortran 2003 auto-allocate print *, 'writing to ',filename open(newunit=u, file=filename, form='formatted') else u = stdout endif i = 3 ! test data write(u,*) i, i**2, i**3 if (u /= stdout) close(u) ! closing stdout can disable text console output, and writes to file `fort.6` in gfortran print *,'goodbye' ! end program implies closing all file units, but here we close in case you'd use in subprogram (procedure), where the file reference would persist. end program
Non-advancing stdout/stdin (for interactive Fortran prompts)