Can a C program be written without main ()?

Can a C program be written without main ()? by Saquib Rais

Answer by Saquib Rais:

Yes, you can write a C program without main().

Here is the universally acclaimed “Hello World” program without main().

#include <stdio.h>
extern void _exit(register int);
int _start(){
printf(“Hello World\n”);

We are taught in almost every book that main () is the entry point of a program execution. It is not completely true. At an abstract level, it is true just from the perspective of programmer. But from systems perspective _start() is the first function that is called. _start() sets up the environment for program execution and then calls main().

Here is the output of above program.

Edit 1: Here is some more info on why I have written extern void _exit(register int);

_start() and _exit() are functions which “system” calls automatically before program execution to setup the environment and do some other machine level work. _exit() is predefined and so we have used external storage class (i.e., extern) to tell the compiler that it has already been declared somewhere. If we miss out extern keyword we are trying to overload the function which is not valid in C language.

_exit() works at machine level so it takes input having a data type of register storage class, hence register keyword is used. If you don’t write the extern void _exit(register int), the compiler will give you just a warning but the program will work fine 🙂

Edit 2: I got this message today:

how do you came to know that _start is called first then it calls the main()?

my sole purpose of asking this question is that from where i can get these details about programming?

In every book we can easily find syntax and all language constructs but these things kind of details are missing.

can you please help me/ guide me from where i can learn about these minute details?

Actually this is a very good question that’s why I am updating my answer, but the answer is very simple than you might think. Everything is right before us, we just need to pay more attention to minute details which we usually ignore.

So to let you know how did I know about _start, I will show you this using an example. Here is a simple program I wrote which does not have main().

Now let’s compile and run this program and see what happens.

It says In function _start: undefined reference to main. It means there is no reference to main() in _start().

That’s when I came to know about _start. Then just a little help with google I found out what this function really is and it is the function which is called first even before main(). So you see, we just have to stop ignoring the errors or warnings and should look for what they actually mean. The only teacher who can teach you things and make you learn is you yourself 🙂

Can a C program be written without main ()?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s