Normally, passing numeric data does not present a problem. If a C program passes an unsigned data type to a Fortran routine, the routine can accept the argument as the equivalent signed data type, but you should be careful that the range of the signed type is not exceeded.
The table of Equivalent Data Types summarizes equivalent numeric data types for Fortran and C/C++.
C and C++ do not directly implement the Fortran types COMPLEX(4), COMPLEX(8), and COMPLEX(16). However, you can write structures that are equivalent. The type COMPLEX(4) has two fields, both of which are 4-byte floating-point numbers; the first contains the real-number component, and the second contains the imaginary-number component. The type COMPLEX is equivalent to the type COMPLEX(4). The types COMPLEX(8) and COMPLEX(16) are similar except that each field contains an 8-byte or 16-byte floating-point number respectively.
Note
Fortran functions of type COMPLEX place a hidden COMPLEX argument at the beginning of the argument list. C functions that implement such a call from Fortran must declare this hidden argument explicitly, and use it to return a value. The C return type should be void.
Following are the C/C++ structure definitions for the Fortran COMPLEX types:
struct complex4 { float real, imag; }; struct complex8 { double real, imag; };
A Fortran LOGICAL(2) is stored as a 2-byte indicator value (0=false, and the -fpscomp [no]logicals compiler option determines how true values are handled). A Fortran LOGICAL(4) is stored as a 4-byte indicator value, and LOGICAL(1) is stored as a single byte. The type LOGICAL is the same as LOGICAL(4), which is equivalent to type int in C.
You can use a variable of type LOGICAL in an argument list, module, common block, or global variable in Fortran and type int in C for the same argument. Type LOGICAL(4) is recommended instead of the shorter variants for use in common blocks.
The Intel C++ class type has the same layout as the corresponding C struct type, unless the class defines virtual functions or has base classes. Classes that lack those features can be passed in the same way as C structures.
If a Fortran program expects a procedure to return a COMPLEX DOUBLE COMPLEX value, the Fortran compiler adds an additional argument to the beginning of the called procedure argument list. This additional argument is a pointer to the location where the called procedure must store its result.
Example below shows the Fortran code for returning a complex data type procedure called WBAT and the corresponding C routine.
Example of Returning Complex Data Types from C to Fortran
Fortran code:
COMPLEX BAT, WBAT
REAL X, Y
BAT = WBAT ( X, Y )
Corresponding C routine:
ccomp.c
struct _mycomplex { float real; float imag; };
typedef struct _mycomplex _single_complex;
void wbat_ (_single_complex *location, float *x, float *y){
*location->real = *x;
*location->imag = *y;
return;
}
In the above example, the following restrictions and behaviors apply:
The argument location does not appear in the Fortran call; it is added by the compiler.
The C subroutine must copy the result's real and imaginary parts correctly into location.
The called procedure is type void.
If the function returned a DOUBLE COMPLEX value, the type float would be replaced by the type double in the definition of location in WBAT.