2

Possible Duplicate:
C preprocessor and concatenation

can anybody explain with example ?

1

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Isn't IF(0, a, b) mapped to b and IF(1, a, b) mapped to a?
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.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.