Can you help me with a short code that can print itself?

#define q(k)main(){return!puts(#k"\nq("#k")");} q(#define q(k)main(){return!puts(#k"\nq("#k")");}) 

This code can be printed on the screen, but it’s hard for me to read it, especially that two # K, how does it work? I know how #define q (k) 2 * k works, but I really don't know about this code. Please help me analyze this! thanks!

+4
source share
1 answer

Simplify the call and use the compiler preprocessor to find out what happens:

 #define q(k)main(){puts(#k"hello("#k")");} q(argument) 

Running gcc -E , which gives you:

 main(){puts("argument""hello(""argument"")");} 

As you can see, what happens is that the q macro argument is converted to a string (because is is used as #k - this is sometimes called a "string"). There is no other magic here.

+8
source

Source: https://habr.com/ru/post/1382127/


All Articles