// this file illustrates macro definitions and compares them to // parameter passing in functions, both by value and by reference. #include #define TRUE 1 // TRUE is not a variable!! #define FALSE 0 #ifndef PI #define PI 3.1415927 #endif #ifndef NULL #define NULL 0 #endif #define PSWAP(type,x,y) { type temp; \ temp = x; x = y; y = temp; } void fswap(int x, int y) { int temp; temp = x; x = y; y = temp; } void rswap(int *x, int *y); // function to be defined later void main() { int a, b; double u, v; a = 4; b = 5; u = PI; v = PI*PI; // PSWAP(int,a,b) // can't call functions like this! // #undef PSWAP // PSWAP can't be used after this point // PSWAP(double,u,v) // cout << "\na: " << a << ", b: " << b; // cout << "\nu: " << u << ", v: " << v; rswap(&a,&b); cout << "\nafter rswap: a: " << a << ", b: " << b; cout << "\n"; } // can't put macro definitions here. Why? // functions are fine though, as long as they were declared earlier void rswap(int* x, int* y) { int temp; temp = *x; *x = *y; // what if I did x = y? *y = temp; }