The __fortran, __pascal, and __cdecl keywords and the /Gc and /Gd options allow you to control the function-calling and naming conventions so that your C programs can call and be called by functions that are written in FORTRAN or Pascal.
Because functions in C programs can take a variable number of arguments, C must handle function calls differently from languages such as Pascal and FORTRAN. Pascal and FORTRAN usually push actual parameters to a function in left-to-right order so that the last argument in the list is the last one pushed onto the stack. In contrast, because C functions do not always know the number of actual parameters, they must push their arguments from right to left, so that the first argument in the list is the last one pushed.
In C programs, the calling function must remove the arguments from the stack. In Pascal and FORTRAN programs, the called function must remove the arguments. If the code for removing arguments is in the called function (as in Pascal and FORTRAN), it appears only once; if it is in the calling function (as in C), it appears every time there is a function call. Because a typical program has more function calls than functions, the Pascal/FORTRAN method results in slightly smaller, more efficient programs. The compiler can generate the Pascal/FORTRAN calling convention in several ways.
Using __pascal and __fortran Keywords
You can use the __pascal and __fortran keywords with functions or pointers to functions to specify a function that uses the Pascal/FORTRAN calling convention. In the following example, sort is declared as a function using the alternative call-ing convention:
short pascal sort(char *, char *);
The __pascal and __fortran keywords can be used interchangeably. Use them when you want to use the left-to-right calling sequence for selected functions only.
/Gc (Use the Pascal/FORTRAN Calling Convention)
If you use the /Gc option, the entire module is compiled using the Pascal/ FORTRAN calling convention. You might use this method to make it possible to call all the functions in a C module from another language or to gain the performance and size improvement provided by this calling convention.
When you use /Gc to compile a module, the compiler assumes that all functions called from that module use the Pascal/FORTRAN calling convention, even if the functions are defined outside that module. Therefore, using /Gc would usually mean that you cannot call or define functions that take variable numbers of parameters and that you cannot call functions such as the C library functions that use the C calling sequence. In addition, if you compile with the /Gc option, either you must declare the main function in the source program with the __cdecl keyword, or you must change the startup routine so that it uses the correct naming and calling conventions when calling main.
/Gd (Use the C Calling Convention)
The /Gd option has the same effect as the __cdecl keyword. It specifies that the entire module should use the C calling convention. This option is on by default.
The __cdecl keyword in C is the inverse of the __fortran and __pascal keywords. When applied to a function or function pointer, __cdecl indicates that the associated function is to be called using the usual C calling convention. This allows you to write C programs that take advantage of the more efficient Pascal/ FORTRAN calling convention while still having access to the entire C library, other C objects, and even user-defined functions that accept variable-length argument lists. The __cdecl keyword takes precedence over the /Gc option.
For convenience, the __cdecl keyword has already been applied to run-time-library function declarations in the include files distributed with the compiler. Therefore, your C programs can call the library functions freely, no matter which calling conventions you compile with. Make sure to use the appropriate include file for each library function the program calls.
Use of the __pascal and __fortran keywords or the /Gc option also affects the naming convention for the associated item (or, in the case of /Gc, all items): the name is converted to uppercase letters, and the leading underscore that C usually prefixes is not added. The __pascal and __fortran keywords can be applied to data items and pointers, and also to functions; when applied to data items or pointers, these keywords force the naming convention described previously for that item or pointer.
The __fastcall naming convention uses the function name preceded by an at sign (@). No case translation is done. When using this convention, make sure to use the standard include files. Otherwise, you get unresolved external references.
The __pascal, __fortran, __fastcall, and __cdecl keywords, like the __based, __near, __far, and __huge keywords, are disabled by use of the /Za option. If this option is given, these names are treated as ordinary identifiers, rather than keywords.
Examples
int __cdecl var_print(char*,...);
In this example, var_print is declared with a variable number of arguments using the usual right-to-left C function-calling convention and naming conventions. The __cdecl keyword overrides the left-to-right calling sequence set by the /Gc option if the option is used to compile the source file in which this declaration appears. If this file is compiled without the /Gc option, __cdecl has no effect since it produces the same result as the default C convention.
float __pascal nroot(number, root);
This example declares nroot to be a function returning a pointer to a value of type float. The function nroot uses the default calling sequence (left-to-right) and naming conventions for Microsoft FORTRAN and Pascal programs.