Preprocessor directives involving concatenation

#include"stdafx.h"
#include<iostream>
#define JUST_CHECKING //preprocessor directives
#define LIMIT 4
#define TEXT 1
#define BOOK 2
#define WORK 3
#define HOME 4
#define TEXTBOOK 5
#define WORKBOOK 6
#define HOMEWORK 7
#define CAT(x,y) x##y //macro for concatenation (use of ## operator)
using std::cout; //program uses cout
using std::endl; //program used endl
int main(void)
{
int i;
int total = 0;
for (i = 1; i <= LIMIT; i++)
{
total += 2*i*i + 1;
#ifdef JUST_CHECKING //conditional compilation
cout<<"i="<<i<<"\trunning total = "<<total<<"\n"<<endl;
#endif
}
cout<<"Grand total = "<<total<<"\n"<<endl;
cout<<"Now let's try concatenation\n"<<endl;
cout<<CAT(TEXT,BOOK)<<"\n"<<endl; //using macro for concatenation
cout<<CAT(HOME,WORK)<<"\n"<<endl;
cout<<CAT(WORK,BOOK)<<"\n"<<endl;
cout<<CAT("WORK","BOOK")<<"\n"<<endl;

}



Previous
Next Post »

Comments:

Disqus Shortname