Effect of extern “C” in C++
The effect of extern “C” in C++ is that it tells the compiler to use the C-style naming convention and linkage rules for the declared functions and variables. This is necessary because C++ has different name-mangling and linkage rules than C.
When you declare a function or variable using extern “C”, the compiler generates a symbol name that follows the C naming convention and linkage rules, making it compatible with C libraries and other programs that use C linkage. This allows C++ programs to call functions from C libraries.
The main purpose of the extern “C” keyword is to enable interoperability among programs written in different languages. Without it, C++ would not be able to “talk” to programs written in other languages such as Java, C#, and many others. With this keyword, an object declared in C++ code can be accessed by a program written in another language, reducing the barriers to communication between different programs. This can allow programs written in different languages to have more intertwined functionality, which can be very useful in complex applications.
The extern “C” keyword can also have an indirect effect on the functionality and performance of a program written in the C++ language. A program written in C++ may have portions written in a different language, such as assembly code. The extern “C” keyword can be used to access these non-C++ functions from a C++ program. Using this keyword to access functions written in a different language can reduce the overall complexity of the program, resulting in greater clarity, faster execution speeds, and generally easier debugging.
Moreover, the extern “C” keyword can be used to facilitate third-party integration. If a company or organization develops an external module or tool in a language other than C++, the “extern C” keyword can be used to access functions and objects from that module or tool in the C++ code. This makes the external module or tool easier to use and ensures compatibility with the C++ program.
Let’s have a look at a simple example for getting the value of int variable in the output.
void func(int a)
{
std::cout << "The value of a is " << a << std::endl;
}
int main() {
func(35);
return 0;
}
If we run the above code C++ code in the compiler, it outputs the value of a as 35.
However, if we want to call this same function inside a C program, we will run into a problem.
extern void func(int);
int main() {
func(35);
return 0;
}
To fix the above error, we must use the extern “C” keyword to acknowledge the C++ compiler to generate a C-style function name for the above function so that the C compiler will recognize the code.
extern "C" void func(int a) {
std::cout << "The value of a is " << a << std::endl;
}
int main() {
fun(35);
return 0;
}
Conclusion
The extern “C” keyword is one of the most useful keywords to have in any C++ language. It allows users to integrate C++ code with C code or when writing C++ libraries that are developed for C programming. The above-mentioned guideline provides useful information to learn the effect of the extern “C” keyword with sample codes to clearly show how extern “C” is used in C++.