CERN Accelerating science

This website is no longer maintained. Its content may be obsolete. Please visit for current CERN information.

next up previous
Next: 1- Type Definitions Up: Tutorial Section Previous: Tutorial Section

Interfacing Fortran and C


In this articlegif I shall summarize the experience about calling C from Fortran and vice versa.

Over the past two years the originally Fortran-based library package KUIP has been gradually rewritten in C. (KUIP stands for ``Kit for a User Interface Package'' and provides the user interface for PAW and other applications from the CERN program library. KUIP has been ported to VMS, VM/CMS, MVS, MS-DOS, Windows/NT, and a large number of Unix flavours.)

The rewrite was not a pure ``exercise of style'' but it appeared to be the best choice in order to improve the quality of the package. Key elements for a user interface package such as dynamic string handling, recursivity, and function pointers are inherent to the C language. Using C allowed the removal of most of the limitations with respect to maximum string lengths built into the original Fortran code. It also facilitated the interface to Motif which is only accessible through C libraries.

The Fortran routines of KUIP have been replaced by C functions. The C code emulates the former Fortran entry points and is therefore completely compatible.

When doing inter-language calls between Fortran and C we have to realize that the Fortran part is fixed, and that the C code has to adapt to the characteristics employed by the Fortran compiler. The cfortran.hgif header file by Burkhard Burow provides convenient preprocessor macros for interfacing Fortran and C. For reasons explained below it does, however, not cover the IBM mainframe systems. Also, cfortran.h exercises cpp to the full which always involves the risk of stretching the C preprocessor beyond its limits.

By no means we do want to discourage the use of cfortran.h. However, if cfortran.h cannot be used (because the target platform is not supported or the overhead is considered too large), here you should find the necessary information for doing the Fortran-C interface ``by hand''.

next up previous
Next: 1- Type Definitions Up: Tutorial Section Previous: Tutorial Section

Janne Saarela
Mon May 22 15:43:10 METDST 1995