-1

Is there any way to write a C program without a main function? If so, how can that be achieved?

Bart
  • 19,692
  • 7
  • 68
  • 77
Vinoth K
  • 465
  • 1
  • 10
  • 19

5 Answers5

8

C defines the entry point in a hosted environment to be main. In a "freestanding" environment, however, the entry point can have some other name. That's about the only latitude the language (at least officially) allows in that particular respect.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
7

Yes, you can.

_start function is the entry point of a C program which makes a call to main().

Going further into it, main() is the starting point of a C program from the programmer's perspective. Before calling main(), a process executes a bulk of code to "clean up the room for execution".

_start is the function which gets called first, which then allocates necessary resources and then calls main() which has to be defined by the programmer.

You can override _start and tell the compiler to not to look for main() by using "-nostartfiles" option.

#include <stdio.h> //for using printf()
_start()
{
    printf("Hello world!!\n");
    _exit(0);
}

To Compile : gcc -nostartfiles code.c -o a.out

Also look at http://linuxgazette.net/issue84/hawk.html for more basic information.

EsmaeelE
  • 2,331
  • 6
  • 22
  • 31
Sumant Kumar Mehta
  • 115
  • 1
  • 3
  • 5
3

No. C is totally based off of the assumption that you start the program in main(). Anyway, why would you want this? This would make inconsistencies for other programmers reading your code.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • 3
    Sort of. That's true for *hosted* environments, but *freestanding* environments are free to deal with program startup however they want. Of course, then it's entirely implementation-defined territory. – jamesdlin Sep 08 '10 at 10:31
3

The following linker abuse

char main[] = { /* Machine code for your target implementation */ };

will work on some platforms.

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
  • This is not in .data segment, on windows UAC must be terminate this application of executing code from non .data segment. – Svisstack Sep 08 '10 at 10:25
  • 1
    +1 hehe, nice hack of the question ;-) – Michael Sep 08 '10 at 10:36
  • @Michael, yep but will work only on a few platforms (see @Svisstack's comment) – Alexandre C. Sep 08 '10 at 10:58
  • I don't think that this abuse is covered by the standard. There it clearly states that `main` must be a function. Function pointers and data pointers are incompatible in C99. – Jens Gustedt Sep 08 '10 at 11:22
  • @Jens: Linkers usually don't care about the C standard, `main` is just a symbol. – Alexandre C. Sep 08 '10 at 13:39
  • Linkers may accept it, but that doesn't make this a C program. Linkers have been known to even accept * gasp * C++ programs. – MSalters Sep 08 '10 at 13:51
  • @Alexandre C: first you said, that this is a valid C program, which it isn't, so your answer is at least misleading, in not pointing to a proper solution but to a hack. And then, more seriously, on a given platform function pointers may just be wider than data pointers and just anything may happen if you try to execute such a "symbol" – Jens Gustedt Sep 08 '10 at 14:34
  • @Jens: of course this is a hack, and it won't work on some (many?) platforms. I correct my answer to match your criticism. – Alexandre C. Sep 08 '10 at 14:39
2

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

An alternative is to write a C program and look at the Assembly output: http://users.aber.ac.uk/auj/voidmain.shtml

More information about what happens before main() is called can be found here (How Initialization Functions Are Handled): http://gcc.gnu.org/onlinedocs/gccint/Initialization.html

Codeape
  • 288
  • 3
  • 14