4. INTER-PROCEDURE INTERFACE
To be able to write C procedures that call or are called by Fortran procedures,
it is necessary to know the conventions for procedure names,
data representation,
return values,
and argument lists that the compiled code obeys.
4.1. Procedure Names
On
UNIXsystems,
the name of a common block or a Fortran procedure
has an underscore appended to it by the compiler
to distinguish it from a C procedure or external variable
with the same user-assigned name.
Fortran library procedure names have embedded underscores to avoid clashes
with user-assigned subroutine names.
4.2. Data Representations
The following is a table of corresponding Fortran and C declarations:
Fortran C
integer*2 x short int x;
integer x long int x;
logical x long int x;
real x float x;
double precision x double x;
complex x struct { float r, i; } x;
double complex x struct { double dr, di; } x;
character*6 x char x[6];
(By the rules of Fortran,
integer,
logical,
and
real
data occupy the same amount of memory).
4.3. Return Values
A function of type
integer,
logical,
real,
or
double precision
declared as a C function that returns the corresponding type.
A
complex
or
double complex
function is equivalent to a C routine
with an additional
initial argument that points to the place where the return value is to be stored.
Thus,
-
complex function f( . . . )
is equivalent to
-
f_(temp, . . .)
struct { float r, i; } *temp;
. . .
A character-valued function is equivalent to a C routine with
two extra initial arguments:
a data address and a length.
Thus,
-
character*15 function g( . . . )
is equivalent to
-
g_(result, length, . . .)
char result[ ];
long int length;
. . .
and could be invoked in C by
-
char chars[15];
. . .
g_(chars, 15L, . . . );
Subroutines are invoked as if they were integer-valued functions
whose value specifies which alternate return to use.
Alternate return arguments (statement labels) are not passed to the function,
but are used to do an indexed branch in the calling procedure.
(If the subroutine has no entry points with alternate return arguments,
the returned value is undefined.)
The statement
-
call nret(*1, *2, *3)
is treated exactly as if it were the computed
goto
-
goto (1, 2, 3), nret( )
4.4. Argument Lists
All Fortran arguments are passed by address.
In addition,
for every argument that is of type character or
that is a dummy procedure,
an argument giving the length of the value is passed.
(The string lengths are
long int
quantities passed by value).
The order of arguments is then:
-
Extra arguments for complex and character functions
Address for each datum or function
A long int for each character or procedure argument
Thus, the call in
-
external f
character*7 s
integer b(3)
. . .
call sam(f, b(2), s)
is equivalent to that in
-
int f();
char s[7];
long int b[3];
. . .
sam_(f, &b[1], s, 0L, 7L);
Note that the first element of a C array always has subscript zero,
but Fortran arrays begin at 1 by default.
Fortran arrays are stored in column-major order, C arrays are stored in row-major order.