Calling C from FORTRAN and Pascal
A C function is called from a FORTRAN or a Pascal program in a manner similar to the way any other type of function is called from FORTRAN or Pascal. The main program must be told that the function is written in C (so that the parameters are passed properly), and C library functions must not be called.
Because C is not the language of the main program, the C initialization code cannot be called, which means the C library functions cannot be called. This restriction can limit the usefulness of a C function; you may decide that it is easier to write the entire program in one language.
All the Things that Can Go Wrong
When you mix languages in your programming, you can easily get things mixed up. Much of the discussion in this section is specific to Microsoft’s C compilers. However, other compilers often behave in a similar manner. Borland’s C compilers, for example, offer similar methods for argument passing.
Following are some of the more common things that can ruin your day:
If you are calling a C function from another language, you must tell the compiler the calling convention. You can tell C that a function will be using FORTRAN calling conventions, then call the function from a FORTRAN program without telling the FORTRAN program that the function is written in C. If the function is written in C with C’s calling conventions, however, the calling program must know this.
When assembly is called from C, the parameters must be read from the stack in the correct order. A C function expects its arguments to be passed in right to left order. A FORTRAN or a Pascal function expects arguments in a left to right order.
A function using the C calling conventions expects its caller to clean the arguments from the stack. A FORTRAN or a Pascal function takes the arguments from the stack itself.
The value being returned must be in the correct place. Generally, AX is used for 2-byte return values, and AX and DX are used for 4-byte return values.