Handling Numeric, Complex, and Logical Data Types

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, MASM, and Visual Visual C/C++.

C, Visual C++, and MASM do not directly implement the Fortran types COMPLEX(4) and COMPLEX(8). 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 type COMPLEX(8) is similar except that each field contains an 8-byte floating-point number.


Note: On ia32 systems, 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 Visual C/C++ structure definitions for the Fortran COMPLEX types:

 struct complex4 {
    float real, imag;
 };
 struct complex8 {
    double real, imag;
 };

The MASM structure definitions for the Fortran COMPLEX types follow:

  COMPLEX4 STRUCT 4
    real REAL4 0
    imag REAL4 0
  COMPLEX4 ENDS
  COMPLEX8 STRUCT 8
    real REAL8 0
    imag REAL8 0
  COMPLEX8 ENDS

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 Visual 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.