how does a call from c++ to c work internally??
2 Answers
It is a heavy duty implementation detail. But most C++ compilers I know don't try to do anything special to differentiate a C function from a non-instance C++ function. Just the plain olden cdecl calling convention for both.
Kinda important because the CRT implementation, with functions like printf(), are just as usable by a C compiler as they are by the C++ compiler from the same vendor. Nobody wants to maintain two versions of it.
3 Comments
cHao
For reference, how does
extern "C" factor in?Scott Wales
@cHao it stops the name mangling that c++ does to support operator overloading, e.g.
foo(int) and foo(double) might translate to foo_int and foo_double in the assembly emitted by the compiler, but extern "C" foo(int) and extern "C" foo(double) both translate simply to fooHans Passant
It changes the name of the identifier as seen by the linker. Turns off the C++ name mangling. All traditional CRT functions are extern "C" in their declaration as seen by the C++ compiler.