Using the C header file (function) in C++ (C++ wrapper) program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the stdio.h header file in C++ program example as C++ wrapper, making the C functions available in C++ program

To show: How to use the C header file (function) in C++ program as C++ wrapper so that the C functions are available in C++ program

 

// using C header file in C++ code as C++ wrapper

#include <cstdio>

 

// main() function

int main(void)

{

// variables declaration and initialization

int x, y, z;

x = 20;

y = 2;

 

printf("\nGiven x = 20, y = 2\n");

printf("\nx / y = %d", x / y);

 

x = x * y;

y = y + y;

 

printf("\nx = x * y");

printf("\ny = y + y");

printf("\nNew value for x / y = %d", x / y);

z = 20 * y / x;

printf("\nz = 20 * (y / x )= %d\n", z);

 

return 0;

}

 

Output example:

 

Given x = 20, y = 2

x / y = 10

x = x * y

y = y + y

New value for x / y = 10

z = 20 * (y / x )= 2

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index