The well-known #define
and #if..#endif
is no big deal. But from time to
time, we might want to use some features that provided by the CPP.
Concatenation of tokens
All input to CPP are text strings and therefore we can do concatenation of strings like this:
#define NAMEVALUE(x,y,z) x##y(z)
Then by doing
NAMEVALUE(pri,ntf,"hello world\n");
it resolves to
printf("hello world\n");
Quotes
To put stuff as string, we are not using "x"
but #x
, for example
#define DEBUGPRINT(x) printf("%s = %d\n",#x,x)
Then doing
DEBUGPRINT(x);
will output something like
x = 3
Predefined Macros
__FILE__
resolves into the filename of the source code file.__LINE__
resolves into the source line number__DATE__
and__TIME__
resolves into the compile date and time__func__
resolves into the function name, only in C99