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.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>