FORTRAN77 Program Calls C Function

F77_CALLS_C is a directory which demonstrates how a FORTRAN77 program can call a C function in a way that might work; because FORTRAN77 is limited in some ways, it actually can be difficult or impossible to call a C function, especially if the interface to that C function is not under the control of the user.

Some reference books discuss this topic by showing little isolated pieces of code, which do not form a real program, and cannot actually be compiled or used. Since EVERYTHING has to be correctly written and working properly together to make this delicate operation happen, it is very important to have an entire calculation in mind, and to be able to examine the full FORTRAN77 and C source code, as well as the compile and load statements used.

The KRONROD example presented here involves a FORTRAN77 main program and a library of 4 C routines. For comparison, you can also look at a directory where the same calculation is done with the main program and library routines written in the same language. Simply go to the KRONROD directory for the language you are interested in.

Perhaps the trickiest issue here is the difference in conventions for dealing with a scalar argument passed to a function. A C function will typically expect such an argument to be passed by value; it is also possible to pass the quantity by reference, although this is more unusual. If the C function already exists and cannot be changed by the user, then this can present a difficulty. The reason is that FORTRAN77 only provides the option of passing scalar arguments by reference.

VAX FORTRAN, and the GFORTRAN compiler provide a function %VAL(variable) which essentially instructs the compiler to use call-by-value instead of call-by-reference for the indicated argument. In other words, a FORTRAN program might call a C function with the statement

kronrod ( %val ( n ), %val ( eps ), x, w )
where n and eps are scalar values, while x and w are presumably vectors for which call-by-reference is appropriate and typically used automatically by both C and FORTRAN.

If your FORTRAN77 compiler does not provide some way to override the call-by-reference convention, then it can be almost impossible to properly communicate data directly between a FORTRAN calling program and a C function.


The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.


F77_CALLS_C is available in a FORTRAN77 version and a FORTRAN90 version and a MATLAB version.

Related Data and Programs:

C_CALLS_F77, C programs which illustrate a C program calling a FORTRAN77 subroutine.

C++_CALLS_F77, C++ programs which illustrate how a C++ program can call a FORTRAN77 subroutine.

F77_CALLS_C++, FORTRAN77 programs which issue a call to a C++ function.

F77_CALLS_MATLAB, FORTRAN77 programs which issue a call to MATLAB to carry out an auxillary calculation.

F77_RETURN, FORTRAN77 programs which illustrates how a FORTRAN77 program can return a program status value to the calling environment.

F90_CALLS_C, FORTRAN90 programs which illustrates how a FORTRAN90 program can call a C function.

MATLAB_CALLS_C, MATLAB programs which illustrate how C functions can be written, compiled, and called from MATLAB using the MEX facility;

MIXED, FORTRAN77 programs which call a function written in another programming language.


Source Code:

The KRONROD example involves a FORTRAN77 main program which calls directly the C functions "kronrod" and "timestamp".

You can go up one level to the FORTRAN77 source codes.

Last revised on 21 December 2010.