1

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?

Shahbaz
  • 46,337
  • 19
  • 116
  • 182

3 Answers3

3

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.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • It's always amusing to see people say that some puzzle solution is "impossible". – Jim Balter Feb 21 '11 at 12:39
  • @Jim, the problem as defined is not possible in a hosted C environment, as the C standard requires a main function in such an environment. It's possible my interpretation of the problem, or my assumptions about the environment are wrong. For example, if the problem is actually "The text `main` shall not appear in the program" you can do silly preprocessor hacks. If this is a win32 environment, you can use a non-console subsystem (a freestanding environment) and `winmain`. But I can't read minds, so I have to assume what the correct interpretation of the constraints should be. – bdonlan Feb 21 '11 at 12:48
  • @bdonlan "if the problem is actually ..." -- which, **of course**, it is, because quine **puzzles** are always about the program text, not about going and looking at the cpp or asm or obj or exe, where you can find main but are irrelevant. – Jim Balter Feb 21 '11 at 12:55
  • @bdonlan BTW, the C standard doesn't "require" anything. It merely **specifies** what is or is not a conforming program or implementation. There are all sorts of non-conforming programs and non-conforming implementations and the OP didn't state conformance as an explicit requirement. It didn't even specify a hosted environment. I think a Linux kernel -- which does not have a main() -- that quined itself under some circumstances would be quite nifty. Puzzles, especially such as quines, need some thinking out of the box, and one should not be too quick to say "impossible". – Jim Balter Feb 21 '11 at 13:01
  • A Linux kernel is a freestanding environment, where none of the rules apply. If the OS just specifies "C" we need to assume a hosted environment; otherwise, we don't even know how to emit output unless the environment has been specified. Yes, you might be able to pick an environment out of a hat where you don't need a `main`, but chances are it won't be the environment the OP was hoping for. – bdonlan Feb 21 '11 at 13:17
  • Also, I'd like to stress here - preprocessor hacks to define main are _still defining main_. It's unclear whether the OP wants truly no `main`, or simply no instance of the text `main`. The OP needs to more clearly specify the problem - until they do, I'm sticking with my interpretation. – bdonlan Feb 21 '11 at 13:19
1

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?

Community
  • 1
  • 1
Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155
0
#include <stdio.h>

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

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

There is main() declaration, but not definition.

blaze
  • 4,326
  • 18
  • 23