+ 1

what is the output of this and explain how it works??

#include <stdio.h> #define TO_STR(x) #x int main() { printf("%s\n"TO_STR(123\\45)); return 0; }

21st Oct 2020, 6:23 PM
mohana hemanth
mohana hemanth - avatar
3 Answers
+ 2
Stringizing Operator # What comes after # will be converted into a string, it means it will be enclosed in double quotes. So TO_STR(123\\45) will be replaced by "123\\45" And ends up like this: printf("%s\n" "123\\45"); Adjacent strings are concatenated so its the same as printf("%s\n123\\45"); But it wont compile since %s specifier requires an argument which is not provided.
21st Oct 2020, 7:18 PM
arturop200
arturop200 - avatar
0
i think the preprocessor substitute every occurrence of TO_STR(x) with #x So your program actually runs: printf("%s\n"#123\\45); However I never saw a #something into a printf, so I don't know exactly what is going to happen
21st Oct 2020, 6:46 PM
Davide
Davide - avatar