HP Fortran Programmer's Guide (B3908-90031; September 2011)

Calling C routines from HP Fortran
Data types
Chapter 8 185
Unsigned integers
Logicals
Complex numbers
Derived types
Unsigned integers
Unlike Fortran, C allows integer data types (char, int, short, and long) to be declared as either signed
or unsigned. If a Fortran program passes a signed integer to a C function that expects an unsigned integer
argument, C will interpret the bit pattern as an unsigned value.
An unsigned integer in C can represent twice the number of positive values as the same-sized integer in
HP Fortran. If an HP Fortran program calls a C function that returns an unsigned integer and the return
value is greater than can be represented in a signed integer, HP Fortran will interpret the bit pattern as a
negative number.
Logicals
C uses integers for logical types. In HP Fortran, a 2-byte LOGICAL is equivalent to a C short, and a 4-byte
LOGICAL is equivalent to a long or int. In C and HP Fortran, zero is false and any nonzero value is true.
HP Fortran sets the value 1 for true.
Complex numbers
C has no complex numbers, but they are easy to simulate. To illustrate this, create a struct type containing
two floating-point members of the correct size — two floats for the complex type, and two doubles for
the double complex type. The following creates the typedef COMPLEX:
typedef struct
{
float real;
float imag;
} COMPLEX;
Consider a program that consists of two source files:
The Fortran source file, which defines the main program unit
The C source file, which defines a function sqr_complex, having the following prototype declaration:
COMPLEX sqr_complex(COMPLEX cmx_val);
The main subprogram calls sqr_complex, passing in a complex number. The C function squares the
number and returns the result. There is no complex data type in C, but this example uses C’s typedef
feature to create one.