Using parentheses in macros. : define « Development « C++ Tutorial






#include <iostream>
using namespace std;
#define CUBE(a) ( (a) * (a) * (a) )
#define THREE(a) a * a * a

int main()
{
   long x = 5;
   long y = CUBE(x);
   long z = THREE(x);

   cout << "y: " << y << endl;
   cout << "z: " << z << endl;

   long a = 5, b = 7;
   y = CUBE(a+b);
   z = THREE(a+b);

   cout << "y: " << y << endl;
   cout << "z: " << z << endl;
 return 0;
}








5.16.define
5.16.1.ASSERTS
5.16.2.function macro of get the max value number
5.16.3.Using parentheses in macros.
5.16.4.Using inline rather than a macro.
5.16.5.A simple assert() macro.
5.16.6.Printing values in DEBUG mode