How to call C function from C++ code?

(Last Updated On: April 10, 2017)

Answer: Calling C function from C++ code is really simple. There may be a chance in software projects where mixing C and C++ code in the same program is required. One of the standard way, is to put C functions into a C header file, in extern “C” block. Notice how C++ macro has been used. For example

 

 

This way, supports for both C and C++ compiler. It specifies the linkage specification that how C or C++ compiler has to link. Whenever C++ compiler, find this macro, it compiles the functions in C –style.

Here is C++ code example to call C function from C++ code :