Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who calls the main function in C [duplicate]

Tags:

c

main

linux

Possible Duplicate:
In C, how is the main() method initially called?

I want to know, who calls the main function in C.
What is actual use of the main function ( why main is special/necessary ) ?
Can I write a c program with out main function ?

like image 238
Vikram Avatar asked Feb 04 '12 21:02

Vikram


2 Answers

The main function is called, in practice, by the C runtime.

You can write a program without main but it must have an entry point. Different operating systems allow you to specify different entry points for your program, but they all serve the same purpose as main. On Windows, you can use WinMain. On Linux you can link without the CRT and define your own _start function (but it cannot return!)

A program without an entry point is like a car without wheels: it doesn't go anywhere.

like image 105
Dietrich Epp Avatar answered Sep 25 '22 16:09

Dietrich Epp


When you ask your operating system to run a file, it loads it into memory, and jumps to it starting point (_start,etc). At this point, there is an code, that call main and then exit (The linker is responsible to this part). If you will write program without main function, the linker will give you an error, since it couldn't find it.

like image 34
asaelr Avatar answered Sep 22 '22 16:09

asaelr