FORTRAN and C [ HP FORTRAN 77/iX Reference ] MPE/iX 5.0 Documentation
HP FORTRAN 77/iX Reference
FORTRAN and C
This section describes how to interface with C.
Logicals
C has no logical type; it uses integers instead. A FORTRAN LOGICAL*2 is
represented by a C short integer, and a LOGICAL*4 by a C long integer.
FORTRAN and C do not share a common definition of true and false. In C,
zero is false, and any nonzero value is true.
Arrays
FORTRAN stores arrays in column-major order, while C stores them in
row-major order.
Files
A FORTRAN unit cannot be passed to a C routine to perform I/O on the
associated file. Nor can a C file pointer be used by a FORTRAN routine.
However, a file created by a program written in either language can be
used by a program of the other language if the file is declared and
opened within the latter program.
C accesses files using its own I/O subroutines and intrinsics. This
method of file access can also be used from FORTRAN instead of FORTRAN
I/O. Be aware that HP FORTRAN 77 on HP 3000 Series 900 MPE/iX uses the
unbuffered I/O system calls read and write (described in the MPE/iX
Reference manual) for terminal I/O, magnetic tape I/O, and direct access
I/O.
Parameter Passing Methods
FORTRAN passes noncharacter parameters by reference, while FORTRAN
character strings are "passed by descriptor." The descriptors are
system-defined and are described under "Character" later in this section.
Therefore all actual parameters in a C call to a FORTRAN routine must be
pointers or variables prefixed with the address operator (&), and all
formal parameters in a C routine called from FORTRAN must be pointer
variables, unless a FORTRAN ALIAS directive defines them as value
parameters. FORTRAN character data passed as parameters or to a C
routine can be handled in a special manner by specifying the C option to
the ALIAS directive, or by using the ALIAS directive with the optional
parameter information list, as described under "ALIAS Directive," later
in this chapter. Alternately, a structure can be defined corresponding
to the FORTRAN character descriptor.
If a FORTRAN program is receiving a parameter from a C program, the
parameter is by reference if it is an array; otherwise, the parameter is
passed by value if it is less than or equal to 64 bits, or by reference
if it is greater.
If a FORTRAN program is passing a parameter to a C program, and the C
program declares it to be of type array, the C program expects the
parameter to be passed by reference. If the C program declares it to be
a structure or union greater than 64 bits, the C program copies the
parameter into a temporary memory location. The parameter-passing
mechanism is then by reference, but the effect is as if by value, because
the value cannot be changed. If the parameter is less than or equal to
64 bits, it is passed by value.
This parameter passing is handled correctly by specifying C in the
language option of the ALIAS directive. (See "ALIAS Directive" earlier
in this chapter.)
Complex Numbers
C has no complex numbers. However, a COMPLEX*8 number can be represented
in C by the following structure:
struct complex {
float real_part, imaginary_part;
}
Similarly, a FORTRAN COMPLEX*16 number can be represented by the same
structure with the real and imaginary parts being of C type double.
Character
When FORTRAN passes character parameters, it passes them by descriptor.
The descriptor includes two items: a pointer to the first character in
the string and an integer value for the declared length of the string.
When passing FORTRAN character strings to C subprograms, you must be sure
to accommodate the length descriptor. Use the default character passing
method to accomodate the length descriptor when passing a character
string to a C subprogram. Note that parameters from FORTRAN are all
passed by reference, except for the character length descriptors, which
are passed by value.
Default Character Passing Method.
When you use the default method of character passing, an integer length
descriptor is passed after each character parameter. Therefore, you need
to provide two variables in the parameter list of the C subprogram for
each FORTRAN character variable passed. For example, if FORTRAN passes
the C subprogram two parameters (two strings and one integer), the C
subprogram must be able to accept five parameters (two strings and three
integers). This is illustrated in the following example:
FORTRAN code:
INTEGER*4 num
CHARACTER*10 str1,str2
CALL testproc (str1,str2,num)
C code:
testproc (str1,strlen1,str2,strlen2,num)
int*num;
char*str1;
char*str2;
int strlen1;
int strlen2;
Hollerith
The FORTRAN Hollerith data type is similar to the C char array.
MPE/iX 5.0 Documentation