Tuesday, April 5, 2011

The # and ## Preprocessor Operators

The # and ## preprocessor operators are used when using a macro #define.

The # operator turns the argument it precedes into a quoted string. For example, given:

#define mkstr(s) # s

the preprocessor turns the line

printf(mkstr(I like C);

into

printf("I like C");

The ## operator concatenates two tokens. For example, given:

#define concat(a, b) a ## b
int xy=10;
printf("%d",concat(x, y);

the preprocessor turns the last line into:

printf("%d", xy);