How would you call a C function from C++ code?


Ans: Using extern “C”.

The function prototype must be preceded by extern “C”. More than one C functions can be grouped inside braces as shown below:

extern “C”

{

void f( ) ;

void f1( ) ;

}

// In cfunc.c

#include <stdio.h>

void f( )

{

printf ( “in f( )” ) ;

}

// In func.cpp

#include <iostream.h>

extern “C” void f( ) ;

void main( )

{

f( ) ;

}

Ensure that both .c and .cpp files are in the same project.

Subscribe / Share

It's very calm over here, why not leave a comment?

Leave a Reply




Categories

Powered by Yahoo! Answers