Search code examples
ccalling-convention

Can function be defined with different calling convention in c?


int _cdecl    f (int x) { return 0; }
int _stdcall  f (int y) { return 0; }

After name mangling will be:

_f
_f@4

Which doesn't conflict, is this allowed in c ,if not, why?


Solution

  • The keywords _cdecl and _stdcall are not part of the C language. These are Microsoft extensions which were preceded by similar Borland extensions.

    In the standard C language, you can't declare a calling convention. Every function declared is, obviously, equivalent to what the MS compiler refers to as the "_cdecl" convention.

    It would be possible to use in-line assembly to distinguish the two functions when you call them. Because you're using a platform-specific vendor extension of C, you might consider using in-line assembly.