tags:

views:

70

answers:

2

Possible Duplicate:
C preprocessor and concatenation

can anybody explain with example ?

+4  A: 

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).

jpalecek
Hi jpalecek thanks for the clarification.
Saran
A: 

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.

Daren Thomas
Hi Daren jpalecek's answer is what i was expecting. Thanks for the heads up.
Saran