Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stringizing operator

Tags:

c

macros

How does the following code compile correctly,

#include <stdio.h>
#define stringer( x ) printf_s( #x "\n" )
int main() {
stringer( "In quotes when printed to the screen" );   
}

isn't it supposed to get expanded into

printf_s(""In quotes when printed to the screen""\n");

which is an error as there are nested double quotes in printf_s??

like image 725
user1232138 Avatar asked May 05 '26 21:05

user1232138


2 Answers

No, the # operator handles character string literals specially. It must \ escape each " in a character string literal that is passed to it. The correct expansion is:

printf_s( "\"In quotes when printed to the screen\"" "\n" );
like image 180
CB Bailey Avatar answered May 08 '26 14:05

CB Bailey


No, it's expanded into

printf_s("\"In quotes when printed to the screen\"" "\n");

which will finally be

printf_s("\"In quotes when printed to the screen\"\n");

and should print

"In quotes when printed to the screen"
like image 31
Jah Avatar answered May 08 '26 14:05

Jah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!