You are here:

C++/main() function

Advertisement


Question
Why we use main() function in C program codes?

Answer

Basically because that what C and C++ require for programs using a hosted implementation (i.e. run on a system under the control of an operating system) require as standard.

It is like asking why your phone requires a certain sort of plug for its charger or headphones - because that is what it has.

In rough simple terms when a program is executed - e.g. by typing the name (or pathname) of the executable on a command line - the operating system will create a new process and load the code (and global static data) for the program into memory. The executable file will indicate what function to execute initially, which typically will not directly be main, but some other function used to initialise the program and setup the program's execution environment. When this is done this code calls the main function as the main entry point to the program.

Of course if you are using a non-hosted, freestanding version of C or C++ - maybe because you are writing an operating system or other low level systems application - then of course you will not have a hosted environment in which the code can run (in the case of writing an operating system your code is going to provide much of a hosted environment!). In these cases you need not use a main function.

In fact the C99 ISO standard document states:

       "5.1.2.1 Freestanding environment
        1 In a freestanding environment (in which C program execution may take
        place without any benefit of an operating system), the name and type of
        the function called at program startup are implementation-defined. ..."

and for the hosted environment:

       "5.1.2.2 Hosted environment
        1 A hosted environment need not be provided, but shall conform to the
        following specifications if present.

        5.1.2.2.1 Program startup
        1 The function called at program startup is named main. The implementation
        declares no prototype for this function. It shall be defined with a return
        type of int and with no parameters:
         int main(void) { /* ... */ }
        or with two parameters (referred to here as argc and argv, though any names
        may be used, as they are local to the function in which they are declared):
         int main(int argc, char *argv[]) { /* ... */ }
        or equivalent or in some other implementation-defined manner."

The C++ standards have similar requirements.

Then there are non-standard platform or vendor specific cases that do not use main. For example, Microsoft Windows programs that are for the Windows sub-system require a WinMain function as the main entry point, and their Visual C++ compiler uses a non-standard wmain entry point for console applications that take an argv array of wide Unicode character strings. There are similar non-main using examples where dynamic or shared object libraries are concerned.

Hope this helps.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.