Who calls the main function in C

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

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

+6
source share
4 answers

When you request the operating system to run the file, it loads it into memory and goes to the starting point ( _start , etc.). At this point there is code that calls main , and then exit (this linker is responsible for this part). If you write a program without the main function, the linker will give you an error message because you could not find it.

+5
source

The main function is called in practice at run time C.

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 perform the same task as main . On Windows, you can use WinMain . On Linux, you can reference without CRT and define your own _start function (but it cannot return!)

A program without an entry point is like a car without wheels: it will not go anywhere.

+7
source

Does your program (which is a series of code included in the functions) have to have the correct starting point?

Something needs to be called first to run the rest.

So, this is the starting point of main , which is called by the parent process in your O / S (whatever that is) and allows your program to work

0
source

The simplest answer is this: the user of your program calls the main function when the application starts. Have you ever used a command terminal? If you know that you can pass arguments to the command. For instance:

 $ grep word myfile 

What happens under the covers, 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 this helps.

0
source

Source: https://habr.com/ru/post/907669/


All Articles