Name
The # Operator
Synopsis
In the macro replacement text, the parameters of the macro may be
preceded by the
operator #
(called
the hash or stringizing operator). In this case, the preprocessor
sets the corresponding argument in quotation marks, thus converting
it into a string.
Example:
#define print_int(i) printf( "value " #i " = %d", i )
If x
and y
are variables with
type int
, then the statement:
print_int(x-y);
is replaced with:
printf( "value ""x-y"" = %d", x-y );
Because consecutive string literals are concatenated, this is equivalent to:
printf( "value x-y = %d", x-y );
Get C Pocket Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.