#define

The #define preprocessor takes the form: #define identifier value

#include <iostream> //allows the use of the cout function in the iostream library file
using namespace std ;

#define PI 3.14
#define RADIUS 20

int main()
{
   int area ;

   area = PI * (RADIUS * RADIUS) ;
   cout << "A circle with a radius of " << RADIUS << "cm has an area of: " << area << "cm." << endl ;

   return 0 ;
}

Compile & run:

A circle with a radius of 20cm has an area of: 1256 cm.

 

 

#define can also be used as a macro which could take arguments:

#include <iostream>
using namespace std;

#define MIN(a,b) ( ( (a) < (b) ) ? a : b )  //using the ? ternary operator

int main ()
{
   int val1 = 22, val2 = 64 ;

   cout <<"The minimum is " << MIN( val1, val2 ) << endl;

    return 0;
}

Compile & Run:

The minimum is 22

Leave a Reply