How can I call a non-system C function f(int,char,float) from my C++ code?
If you have an individual C function that you want to call, and for some reason you don’t have or don’t want to #include a C header file in which that function is declared, you can declare the individual C function in your C code using the extern C syntax. Naturally you need to use the full function prototype: extern “C” void f(int i, char c, float x); A block of several C functions can be grouped via braces: extern “C” { void f(int i, char c, float x); int g(char* s, const char* s2); double sqrtOfSumOfSquares(double a, double b); } After this you simply call the function just as if it was a C++ function: main() { f(7, ‘x’, 3.
Related Questions
- Im reading lines from a file into an array, with this code: char linebuf[80]; char *lines[100]; int i; for(i = 0; i < 100; i++) { char *p = fgets(linebuf, 80, fp); if(p == NULL) break; lines[i] = p; } Why do all the lines end up containing copies of the last line?
- How can I create a C++ function f(int,char,float) that is callable by my C code?
- How can I call a non-system C function f(int,char,float) from my C++ code?