Fixed- vs allocatable-length string return value

@ivanpribec,

The issue is similar to this thread. Ultimately only you can decide on your preferred method. Depending on the processor which includes the hardware, the OS and its stack settings, the compiler, and the run-time, the method with the automatic size string result can prove advantageous. Outside of any extreme concerns with performance, I remain biased toward internal IO. So I suggest looking at the following as well:

   print *, "0 in binary: ", bin(0)
   print *, "42 in binary: ", bin(42)
   print *, "huge(int) in binary: ", bin(huge(0))
contains
   function bin(x) result(str)
      integer, intent(in) :: x
      character(len=max(1,bit_size(x)-leadz(x))) :: str
      write( str, fmt="(b0)" ) x
      return
   end function
end

0 in binary: 0
42 in binary: 101010
huge(int) in binary: 1111111111111111111111111111111

1 Like