Stdout buffer in gfortran

@ShrirajHegde , please never accept at face value what anyone informs you about what Fortran was not “meant to be”. Literally, no one really knows that and no one has or should have the authority on that. Rather, focus on what Fortran can be, and that’ll be what you and each of the practitioners can make it out to be. It shall ever remain an open canvas.

For the specific matter that has piqued your curiosity, you will recall starting with Fortran 2003 standard revision, the language has recognized the near-ubiquitous existence of the C companion processor and has included facilities toward the same. The end result is the perennial potential for Fortran to be a systems programming language also, barring any of the “mundane” notions of purity, of course!

Thus for a situation like the one you present here, when the going gets tough in Fortran, the tough can turn to C!! You can try the same:

   use, intrinsic :: iso_c_binding, only : c_char, c_int
   use stdio_m, only : putchar, puts
   integer(c_int) :: i, r
   do
      do i = 49, 90
         r = putchar( i ) 
      end do
      r = puts(c_char_"")
   end do
end 

C:\temp>p.exe
123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZD
123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZD
123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZD
123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZD
123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZD
123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZD
123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZD
123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZD
123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZD
123456789:;<^C

The above snippet being served by the following to interface with C <stdio.h>:

module stdio_m
   use, intrinsic :: iso_c_binding, only : c_char, c_int
   interface
      function putchar( i ) result(r) bind(C, name="putchar")
         import :: c_int
         ! Argument list
         integer(c_int), intent(in), value :: i
         ! Function result
         integer(c_int) :: r
      end function 
      function puts( str ) result(r) bind(C, name="puts")
         import :: c_char, c_int
         ! Argument list
         character(kind=c_char,len=1), intent(in) :: str(*)
         ! Function result
         integer(c_int) :: r
      end function 
   end interface
end module
4 Likes