Speed up development with full-stack environments for every branch.

Learn More

C++ Examples for Understanding the C Preprocessor

333 Runs 1836 Views 302 Copies
Saved

Saved

tianxiasoft 13

tianxiasoft
published 4 years ago

#include <iostream>

using namespace std;

/******************************************************************************************/
/*  This is an example that calculates the length and area of circle   				  */
/******************************************************************************************/

// Define PI. If it is already defined, undefine it
#ifdef PI
#undef PI
#endif
#define PI						3.141592


// Define macro that calculates the x * x
#define SQR(x)					(x * x)
// Define macro that calculates the length of a circle.
#define LengthOfCircle(r)		(2 * PI * r)
// Define macro that calculates the area of a circle.
#define AreaOfCircle(r)			(PI * SQR(r))

// main function
int main()
{
	// input radius
	float radius;
	cout << "Input the radius of circle : ";
	cin >> radius;


	// print length and area of the circle of radius
	cout << "Length is " << LengthOfCircle(radius) << endl;
	cout << "Area is " << AreaOfCircle(radius) << endl;

	// wait for any key pressing
	cin.get();
	return 0;
}
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).