//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Program to show use of preprocessor macros for dynamic behaviour
#include<iostream>
#include<string>
using namespace std;
#define USE_CAR //Comment or Uncomment this if using preprocessor option
int main()
{
int someNum = 3;
#ifdef USE_CAR
string modeOfTransport("Use a Car");
#elif USE_BUS
string modeOfTransport("Use a Bus");
#elif USE_TRAIN
string modeOfTransport("Use a Train");
#else
string modeOfTransport("Use any mode of transport");
#endif
cout<<"Mode of Transport = "<<modeOfTransport<<" and someNum = "<<someNum<<endl;
return 0;
}
The output is as follows:
In the program above, the statement in the start #define USE_CAR says that the code with USE_CAR should be used during compile time. Instead if USE_BUS is defined, that particular peice of code will be used. If nothing is defined then the behaviour with else will be used.
There is another way to use these preprocessor directives. It is by defining in the properties below. If defined this way then the statement should be omitted from the main program.
The output is as follows in this case:
No comments:
Post a Comment