A simple assert() macro.
#define DEBUG
#include <iostream>
using namespace std;
#ifndef DEBUG
#define ASSERT(x)
#else
#define ASSERT(x) \
if (! (x)) \
{ \
cout << "ERROR!! Assert " << #x << " failed\n"; \
cout << " on line " << __LINE__ << "\n"; \
cout << " in file " << __FILE__ << "\n"; \
}
#endif
int main()
{
int x = 5;
cout << "First assert: \n";
ASSERT(x==5);
cout << "\nSecond assert: \n";
ASSERT(x != 5);
cout << "\nDone.\n";
return 0;
}
ASSERTS
#define DEBUG
#include <iostream>
#ifndef DEBUG
#define ASSERT(x)
#else
#define ASSERT(x) \
if (! (x)) \
{ \
std::cout << "ERROR!! Assert " << #x << " failed\n"; \
std::cout << " on line " << __LINE__ << "\n"; \
std::cout << " in file " << __FILE__ << "\n"; \
}
#endif
int main()
{
int x = 5;
std::cout << "First assert: \n";
ASSERT(x==5);
std::cout << "\nSecond assert: \n";
ASSERT(x != 5);
std::cout << "\nDone.\n";
return 0;
}
First assert:
Second assert:
ERROR!! Assert x != 5 failed
on line 24
in file ASSERTS.cpp
Done.
function macro of get the max value number
#include <iostream>
using namespace std;
#define getmax(a,b) ((a)>(b)?(a):(b))
int main()
{
int x=5, y;
y= getmax(x,2);
cout << y << endl;
cout << getmax(7,x) << endl;
return 0;
}
Printing values in DEBUG mode
#include <iostream>
#define DEBUG
#ifndef DEBUG
#define PRINT(x)
#else
#define PRINT(x) std::cout << #x << ":\t" << x << std::endl;
#endif
int main()
{
int x = 5;
long y = 78l;
PRINT(x);
for (int i = 0; i < x; i++)
{
PRINT(i);
}
PRINT (y);
PRINT("Hi.");
int *px = &x;
PRINT(px);
PRINT (*px);
return 0;
}
x: 5
i: 0
i: 1
i: 2
i: 3
i: 4
y: 78
Hi.": Hi.
px: 0x22ff74
*px: 5
Using inline rather than a macro.
#include <iostream>
using namespace std;
inline unsigned long Square(unsigned long a) { return a * a; }
inline unsigned long Cube(unsigned long a)
{ return a * a * a; }
int main()
{
unsigned long x=1 ;
for (;;)
{
cout << "Enter a number (0 to quit): ";
cin >> x;
if (x == 0)
break;
cout << "You entered: " << x;
cout << ". Square(" << x << "): ";
cout << Square(x);
cout<< ". Cube(" << x << "): ";
cout << Cube(x) << "." << endl;
}
return 0;
}
Using parentheses in macros.
#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;
}