Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++ program that prints its own source code as its output

Tags:

c

quine

Wikipedia says it's called a quine and someone gave the code below:

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

But, obviously you have to add

#include <stdio.h> //corrected from #include <stdlib.h>

so that the printf() could work.

Literally, since the above program did not print #include <stdio.h>, it is not a solution (?)

I am confused about the literal requirement of "print its own source code", and any purpose of this kind of problems, especially at interviews.

like image 406
digit plumber Avatar asked Apr 20 '12 00:04

digit plumber


People also ask

Is it possible to write C program which prints itself?

We have to write a C program which will print itself. So, we can use file system in C to print the contents of the file of which we are writing the code, like we are writing the code in “code 1.

What is the output of C code?

Output is dependent on the compiler. For 32 bit compiler it would be fffffffe and for 16 bit it would be fffe.


1 Answers

The trick here is that most compilers will compile without requiring you to include stdio.h.

They will usually just throw a warning.

like image 71
orlp Avatar answered Sep 30 '22 18:09

orlp