What is the use of ## preprocessor in C [duplicate]
Possible Duplicate:
C preprocessor and concatenation
can anybody explain with example ?
It allows to construct indentifiers from their parts. Eg:
#define CLASS_NAME(name) CLASS__ ## name
would expand CLASS_NAME(alpha)
to CLASS__alpha
. It is vastly used in tricks used by the boost preprocessor library, eg.
#define IF0(a, b) b
#define IF1(a, b) a
#define IF(cond, a, b) IF ## cond(a, b)
which would expand IF(0, a, b)
to a
and IF(1, a, b)
to b
. Also, sometimes, it is used to generate struct and function names (akin to c++ templates).
I'm not sure what you meen by "##" preprocessor.
C has a preprocessor for expanding macros before compile time. This is a first pass on the source code. There are a few different things it is used for:
- including other source / header files (
#include
) - conditionally compiling code (
#ifdef
etc.) - expanding macros (
#define
)- handy for constants
- simple functions
Note, though, that this is not really "C" (though part of the spec) and can cause headaches if you get it wrong. I believe new languages would not do it this way anymore.
精彩评论