Department of Engineering

IT Services

Calling Fortran from C

See the HP C Programmer's Guide[3] if you intend doing this on old HP machines. Rudi Vankemmel ( mentions some general points that are worth noting :-

  1. Fortran uses a column wise storage of matrices while C stores them row wise. This means that when you want to parse a matrix from your C-program to the fortran routine you must transpose the matrix in your program before entering the routine. Of course, any output from such a routine must be transposed again.

    If you omit this step, then probably your program will run (because it has data to compute on) but it will generate wrong answers.

    If you have the Fortran source code (of any routine) then on some platforms you may use compiler directives specifying that the Fortran compiler must use row wise storage. Some platforms support these directives. However watch out with this if you call the same routine from another Fortran routine/program.

  2. Your Fortran compiler may add an underscore to the routine name in the symbol table. Hence in the calling C-program/routine you must add a trailing underscore ! Otherwise the loader will complain about an undefined symbol. However, check your compiler for this. For example the Fortran compiler on VAX-VMS systems does NOT add a trailing underscore (there watch out with the fact that the VAX-Fortran compiler translates everything in uppercase).

  3. Fortran passes its variables by reference. This means that you MUST give adresses in your calling C-program.

  4. Watch out especially with floats and doubles. Make sure that the size of the variable in the calling program is identical to the size in the Fortran routine. This is extremely important on machines with little endian byte ordering. Parsing a float (C-routine) to a real*8 (Fortran) number will not generate SEGV but give wrong results as the data is parsed wrongly.