1

how does a call from c++ to c work internally??

2 Answers 2

3

The C++ compiler 'does the right thing' and uses the correct calling convention for the C function - a lame sounding answer but I don't know that there's much more that can be said!

Sign up to request clarification or add additional context in comments.

Comments

2

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

For reference, how does extern "C" factor in?
@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 foo
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.