You can, of course, use Fortran internal READ for this purpose as well.I wonder if there is an execution performance advantage or disadvantage
in doing so?
The command
gfortran xatoi.f90
where xatoi.f90 has the code below creates an executable. This is neat, but I am surprised that I don't need to use a C compiler. Where is the function atoi coming from?
On 2/23/2022 2:24 PM, Beliavsky wrote:
The command
gfortran xatoi.f90
where xatoi.f90 has the code below creates an executable. This is
neat, but I am surprised that I don't need to use a C compiler. Where
is the function atoi coming from?
gfortran, like many Fortran compilers, uses libc as part of its runtime support for Fortran language semantics, and that means that the C
runtime library gets automatically linked in with Fortran code. If you
happen to make a call whose signature matches a libc routine, it will be found and used, and that's where atoi comes from.
You can, of course, use Fortran internal READ for this purpose as well.
The command
gfortran xatoi.f90
where xatoi.f90 has the code below creates an executable. This is neat, but I am surprised that I don't need to use a C compiler. Where is the function atoi coming from? For a C program (also listed below) there is a line
#include <stdlib.h>
to enable calls to atoi. In general, is an INTERFACE all you need to call a function in the C standard library https://www.csse.uwa.edu.au/programming/ansic-library.html ?
! from "Interoperation of Fortran with C"
program main
use, intrinsic :: iso_c_binding, only: c_char, c_null_char
implicit none
interface
function atoi(in) bind(c)
use, intrinsic :: iso_c_binding
integer(c_int) :: atoi
character(c_char) :: in(*)
end function
end interface
integer :: i
character(len=:,kind=c_char), allocatable :: digits
allocate(character(len=5) :: digits)
digits = c_char_'1234' // c_null_char
i = atoi(digits) ! i gets set to 1234
print*,"i=",i
end program main
On 2/23/2022 2:15 PM, Steve Lionel wrote:
On 2/23/2022 2:24 PM, Beliavsky wrote:I wonder if there is an execution performance advantage or disadvantage
The command
gfortran xatoi.f90
where xatoi.f90 has the code below creates an executable. This is
neat, but I am surprised that I don't need to use a C compiler. Where
is the function atoi coming from?
gfortran, like many Fortran compilers, uses libc as part of its
runtime support for Fortran language semantics, and that means that
the C runtime library gets automatically linked in with Fortran code.
If you happen to make a call whose signature matches a libc routine,
it will be found and used, and that's where atoi comes from.
You can, of course, use Fortran internal READ for this purpose as well.
in doing so?
On 2/23/2022 2:56 PM, Gary Scott wrote:
On 2/23/2022 2:15 PM, Steve Lionel wrote:
On 2/23/2022 2:24 PM, Beliavsky wrote:I wonder if there is an execution performance advantage or
The command
gfortran xatoi.f90
where xatoi.f90 has the code below creates an executable. This is
neat, but I am surprised that I don't need to use a C compiler.
Where is the function atoi coming from?
gfortran, like many Fortran compilers, uses libc as part of its
runtime support for Fortran language semantics, and that means that
the C runtime library gets automatically linked in with Fortran code.
If you happen to make a call whose signature matches a libc routine,
it will be found and used, and that's where atoi comes from.
You can, of course, use Fortran internal READ for this purpose as well.
disadvantage in doing so?
atoi and atof are very efficient since they are hard coded to ASCII and
have severe rules. Fortran internal reads and writes are directed by a format statement and fairly slow.
I wrote an invoicing program for us back in 1976 or so on the Univac
1108. I used formatted writes to write to an intermediate file. After
a minute of cpu time, the job manager killed it. My boss had me change
the formatted writes to unformatted writes. The program ran to
completion in 10 seconds then.
Lynn
On 2/23/2022 2:56 PM, Gary Scott wrote:I wrote my own ascii conversion routines for maximum performance where required. The question was academic.
On 2/23/2022 2:15 PM, Steve Lionel wrote:
On 2/23/2022 2:24 PM, Beliavsky wrote:I wonder if there is an execution performance advantage or
The command
gfortran xatoi.f90
where xatoi.f90 has the code below creates an executable. This is
neat, but I am surprised that I don't need to use a C compiler.
Where is the function atoi coming from?
gfortran, like many Fortran compilers, uses libc as part of its
runtime support for Fortran language semantics, and that means that
the C runtime library gets automatically linked in with Fortran code.
If you happen to make a call whose signature matches a libc routine,
it will be found and used, and that's where atoi comes from.
You can, of course, use Fortran internal READ for this purpose as well.
disadvantage in doing so?
atoi and atof are very efficient since they are hard coded to ASCII and
have severe rules. Fortran internal reads and writes are directed by a format statement and fairly slow.
I wrote an invoicing program for us back in 1976 or so on the Univac
1108. I used formatted writes to write to an intermediate file. After
a minute of cpu time, the job manager killed it. My boss had me change
the formatted writes to unformatted writes. The program ran to
completion in 10 seconds then.
Lynn
For
digits = "1234"
I wrote small programs to read an integer from digits 10^7 times using
each of
read (digits,*) i
read (digits,"(i4)") i
i = atoi(digits)
I forgot to say that atoi and atof have almost no error checking in
them. It is not unusual for a C program to crash in them because the
caller passed a null pointer, etc. I have many guards around my calls
to them in my asDouble and asInteger functions.
...
to enable calls to atoi. In general, is an INTERFACE all you need to call a function in the C standard library https://www.csse.uwa.edu.au/programming/ansic-library.html ?
...
integer(c_short) :: atoi
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 159 |
Nodes: | 16 (0 / 16) |
Uptime: | 99:50:51 |
Calls: | 3,209 |
Files: | 10,563 |
Messages: | 3,009,979 |