HP C Programmer's Guide (92434-90009)

Chapter 5 155
Programming for Portability
Calling Other Languages
Calling FORTRAN
You can compile FORTRAN functions separately by putting the functions you want into a
file and compiling it with the -c option to produce a .o file. Then, include the name of this
.o file on the cc command line that compiles your C program. The C program can refer to
the FORTRAN functions by the names they are declared by in the FORTRAN source.
Remember that in FORTRAN, parameters are usually passed by reference (except
CHARACTER parameters on Series 700/800, which are passed by descriptor), so actual
parameters in a call from C must be pointers or variable names preceded by the address-of
operator (&).
The following program uses a FORTRAN block data subprogram to initialize a common
area and a FORTRAN function to access that area:
double precision function get_element(i,j)
double precision array
common /a/array(1000,10)
get_element = array(i,j)
end
block data one
double precision array
common /a/array(1000,10)
C Note how easily large array initialization is done.
data array /1000*1.0,1000*2.0,1000*3.0,1000*4.0,1000*5.0,
* 1000*6.0,1000*7.0,1000*8.0,1000*9.0,1000*10.0/
end
The FORTRAN function and block data subprogram contained in file xx.f are compiled
using f77 -c xx.f.
The C main program is contained in file x.c:
main()
{
int i;
extern double get_element(int *, int *);
for (i=1; i <= 10; i)
printf("element = %f\n", get_element(&i,&i));
}
struct record (cannot always be done; C
and Pascal use different packing
algorithms)
structure
union record case of… union
a. long double is available only in ANSI mode.
Table 5-5. C Interfacing Compatibility
C HP-UX Pascal FORTRAN