7

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 ?

Community
  • 1
  • 1
Vikram
  • 1,999
  • 3
  • 23
  • 35

4 Answers4

7

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.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • Isn't it the other way arround?The program calls and links to C runtime? – Cratylus Feb 04 '12 at 22:06
  • More like a car without start engine, the car is there, it could do everything it's supposed to, but there's nothing that starts the process – whtlnv Feb 04 '12 at 22:09
  • 1) What do you mean by C runtime ? 2) Do you mean if I write a C program which is executing in my OS perfectly .... I can't run it on different OS because different OS have different entry points ??? – Vikram Feb 04 '12 at 22:12
  • 1
    @vikram: 1) The C runtime is code provided by the C implementation on your platform. 2) All compliant platforms support `main`, so you if you use `main`, you only have to recompile. If you use `WinMain`, your program will not run on non-Windows platforms. – Dietrich Epp Feb 04 '12 at 22:15
  • @user384706: No, the C runtime usually calls main. On Linux, for example, the C runtime provides the `_start` symbol which is the actual entry point for programs. It is not a function, it cannot return because it does not have a caller. This allows separation between the C implementation on your platform and the OS kernel. The kernel doesn't care about your calling conventions so you can use whatever calling conventions you'd like. – Dietrich Epp Feb 04 '12 at 22:22
  • A program without an entry point is like a dynamic library, it has no entry point lmao – Adham Zahran Feb 10 '21 at 18:56
5

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.

asaelr
  • 5,438
  • 1
  • 16
  • 22
0

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

Cratylus
  • 52,998
  • 69
  • 209
  • 339
0

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.

Matthew
  • 12,892
  • 6
  • 42
  • 45