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 ?
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 ?
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.
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.
Your program (which is series of code bundled inside functions) must have to have a starting point right?
Something must be called first to run the rest.
So, that starting point is main
, which is called by the parent process in your O/S (what ever that is) and lets your program run
Simplest answer is this: the user of your program calls the main function when they start your application. Have you ever used a command terminal? If you have you will know that you can pass arguments to a command. For example:
$ grep word myfile
What is going on under the covers is the Terminal looks at what you typed then calls the main
method of the grep program and passes [word, myfile]
as the second argument to this method. This is a simplification but I hope it helps.