开发者

What is the use of ## preprocessor in C [duplicate]

开发者 https://www.devze.com 2023-01-14 13:44 出处:网络
This question already has answers here: C开发者_如何学运维losed 12 years ago. Possible Duplicate:
This question already has answers here: C开发者_如何学运维losed 12 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消