What is the use of main() and why is it compulsory??🤔 | Sololearn: Learn to code for FREE!

+2

What is the use of main() and why is it compulsory??🤔

10/21/2021 2:32:42 AM

Tommy

5 Answers

New Answer

+5

The main function is, by convention, the entry point in most programs. It is not called by the operating system, it is called by the C/C++ starup code. The startup code and runtime libraries are linked together with your code to produce the final executable program. Not every program uses main() as the entry point. The most notable exception is any Windows program. They use WinMain() as the entry point. The old Palm Pilot personal digital assistants (PDAs) used PilotMain() as the entry point. The reason that main, WinMain, or whatever is compulsory is because it is called by the startup code. If the function is missing the linker will be unable to generate the necessary address to link the startup code with your code, the linking phase will fail, and no program will be generated.

+3

the main function is the driver code of the whole c++ program. It indicates the starting point of instructions when the program is executed.

+2

About main function C language https://en.cppreference.com/w/c/language/main_function C++ language https://en.cppreference.com/w/cpp/language/main_function

+1

In C++, the “main()” function is treated the same as every function. The only difference is that the main function is “called” by the operating system when the user runs the program. It is used as entry point of program. Every program starts execution from main() function. Like, other function it has also return type and parameters or signatures. By default, return type of main() in C programming language is void and for C++ is int.

+1

The main is the first executable statement for every programming language From main we can call various userdefined functions or create code based on requirement