A quick review of preprocessors in c++


The following code gives you a quick insight in the possible preprocessors of c++. I like to keep a copy of this.

#define MAX 100
#define MINE MAX*2
#define E_MS “This is an error message\n”
#define L_S “This is a long\
string and thus is extended”
#define ABS(i) (i)>0?i:-(i)

//when error directive encountered, compiling halts
//#error print this error message //don’t want to halt

//when in “”, look usually in current working directory
//if not found, search as if enclosed in <>
//#include “foo.cpp” //cause doesn’t exist
//in the manner of creator of compiler
#include<cstdio>

//undefine the MACRO ME
#undef ME

/*A special preprocessor that I don’t wanna try to use
#line 100 “ask.cpp”
this preprocessor changes the values of macros __LINE__
and __FILE__ to100 and ask.cpp. __LINE__ identifier contains line
number of currently compiled line of code. __FILE__ identifier is a
string containing name of source file being compiled.
*/

//#pragma is an implementation-defined directive that allows
//various instructions to be given to compiler.

#define str(i) #i
#define concat(a,b) a##b

int main(){
#if MAX==100
printf(E_MS);
#elif MINE!=100
printf(“%d”,ABS(MAX));
#else
printf(L_S);
#endif
#ifdef MINE //if the macro is defined
printf(E_MS);
#else
printf(L_S);
#endif
#ifndef ME //if the macro is not defined
#define ME 300
printf(“I am not defined..\n”);
#endif
printf(str(I am great else\n));
printf(“%d”,concat(MA,X));
return 0;
}

If anything not understood, feel free to comment in here…

Advertisements
Explore posts in the same categories: C++

Tags: , ,

You can comment below, or link to this permanent URL from your own site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: