Write a C program which produces its own source code as its output

This is one of the most famous interview questions

One of the famous C programs is…

char*s=”char*s=%c%s%c;main(){printf(s,34,s,34);}”;main(){printf(s,34,s,34);}

So how does it work?

It’s not difficult to understand this program. In the following statement,

printf(f,34,f,34,10);

the parameter “f” not only acts as the format string, but also as a value for the %s specifier. The ASCII value of double quotes is 34, and that of new-line is 10. With these fact ready, the solution is just a matter of tracing the program.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s