compile-fan
compile-fan

Reputation: 17625

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?

Upvotes: 1

Views: 652

Answers (2)

Heath Hunnicutt
Heath Hunnicutt

Reputation: 19467

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.

Upvotes: 4

David Heffernan
David Heffernan

Reputation: 612993

First off, that's not mangling, that's decoration. Mangling is something that happens with C++ compilers because C++ was originally designed to to support overloading using C style link tools.

As to your question, you can't have two functions with the same name. For the purposes of applying that rule, the un-decorated name is used.

Why is this so? I'd imagine it is because decoration and calling conventions are not part of the C standard and are specific to each compiler. I'm pretty sure that C compilers supporting multiple calling conventions only came in to being a number of years after C was invented.

Upvotes: 1

Related Questions