What does ## mean for the C (C ++) preprocessor?

I have a C program:

#define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); } 

when I run only the preprocessor, it expands it as

 { int var12=100; printf("%d",var12); } 

due to which the output is 100.

Can someone tell me how / why the preprocessor extends var##12 to var12 ?

+7
c c-preprocessor stringification
source share
4 answers

nothing unusual: ## tells the preprocessor to combine the left and right sides

see http://en.wikipedia.org/wiki/C_preprocessor#Token_concatenation

+20
source share

because ## is the token concatenation operator for c preprocessor.

Or maybe I do not understand the question.

+4
source share

## Toner Insert Operator

The double-number-sign or token-pasting (##) operator, sometimes called the merge operator, is used in both an object-like and a functional macro. It allows individual tokens to be combined into one token and, therefore, cannot be the first or last token in the macro definition.

If the formal parameter in the macro definition is preceded or followed by a marker operator, the formal parameter is immediately replaced by the unexpanded actual argument. Macro expansion is not performed by argument until replacement.

+3
source share

#define f(g,g2) g##g2

## is used to concatenate two macros in a c-preprocessor. Therefore, before compiling f (var, 12), you should replace the var12 preprocessor and, therefore, you get the result.

+1
source share

All Articles