What does the # and ## means in C preprocessor macro of c++

user2131316 picture user2131316 · Sep 18, 2013 · Viewed 11.7k times · Source

I read the following code:

#define MACRO(abc, def) {#def ## #abc}

char result[10] = MARCO(abc, def);

I know the ## operator is used to merge the two string to one, but what about the # in front of def and abc?

Answer

Pierre Fourgeaud picture Pierre Fourgeaud · Sep 18, 2013

From the standard (emphasis mine):

16.3.2 The # operator [cpp.stringize]

2/ A character string literal is a string-literal with no prefix. If, in the replacement list, a parameter is immediately preceded by a # preprocessing token, both are replaced by a single character string literal preprocessing token that contains the spelling of the preprocessing token sequence for the corresponding argument. [...]

It "stringifies" the token following the #.

Example:

#define STRINGIFY(x) #x

STRINGIFY(foo)  // will be replaced by "foo"