Is there any way to write a C program without a main function? If so, how can that be achieved?
-
3Why would you want to do that? – helpermethod Sep 08 '10 at 10:13
-
2do you have use case where you have this requirement??Or is it a interview question you are looking solution for? – Anil Vishnoi Sep 08 '10 at 10:14
-
2It may not be directly visible in the code but you can use some macros to give the impression that there is no main. If that is what you are looking for, i will have to search the related question asked a few days ago on SO. – Praveen S Sep 08 '10 at 10:15
-
2`#define blah main` Now you can have a c program with `int blah(int argc, char**argv){printf("April Fool!\n");}` – Amarghosh Sep 08 '10 at 11:11
-
@Amarghosh - How about making it more cryptic ;) – Praveen S Sep 08 '10 at 11:26
5 Answers
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.

- 476,176
- 80
- 629
- 1,111
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.

- 2,331
- 6
- 22
- 31

- 115
- 1
- 3
- 5
-
-
First line should say: *Yes, you can, if you use GCC*. This is compiler specific. – Andrejs Cainikovs Jul 03 '17 at 10:04
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.

- 55,009
- 24
- 135
- 201
-
3Sort 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
The following linker abuse
char main[] = { /* Machine code for your target implementation */ };
will work on some platforms.

- 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
-
@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
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

- 288
- 3
- 14