2

Possible Duplicate:
C preprocessor and concatenation

can anybody explain with example ?

Community
  • 1
  • 1
Hemanth
  • 5,035
  • 9
  • 41
  • 59

2 Answers2

8

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
  • 47,058
  • 7
  • 102
  • 144
0

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
  • 67,947
  • 40
  • 154
  • 200