How to write a quine program without main()

820 views Asked by At

I went through all sorts of quine problems, but my task was to get a quine problem without main(), and loops are also forbidden.

Without loop, it is easy, but I can't figure out how to write one without main(). Can anyone help me or provide me with a link?

3

There are 3 answers

6
bdonlan On

You cannot create a (non-freestanding) C program without a main() function. Thus, creating a quine in C without a main() is impossible in the usual sense.

That said, depending on how you define a quine, you might be able to construct a source file which fails to compile, but for which the compile error (on a certain specific compiler) is the contents of the source file.

2
blaze On
#include <stdio.h>

int
foo(void) {
        printf("pong!\n");
        return 0;
}

int main() __attribute__((weak, alias("foo")));

There is main() declaration, but not definition.

3
Vishwanath Dalvi On

First thing its impossible to write program without main function because compiler always starts execution from main() function, without main function linker will not be aware of start of data segment.

Yeah but playing with some tricks with preprocessor you can do it, but this is not a good method to do that.

http://www.gohacking.com/2008/03/c-program-without-main-function.html

This might help you.

Take a look here too:

Is a main() required for a C program?