tags:

views:

495

answers:

6

I have used the code many times as the starting of my program but never knew what it really meant which brought me to the question,What does Int Main() mean in C++ ?

A: 

It's the entry point to your program - the primary, "main" function, as it were.

Michael Petrotta
+2  A: 

The main function is the entry point to your program, meaning that the code there will execute when your program starts. You are declaring the main function to return an integer. You are supposed to return 0 if your program ran successfully and a non-zero value otherwise.

Raul Agrait
+12  A: 

The function main is the first function executed in a compiled C/C++ program (unless you jump through hoops to configure it otherwise). Why is it called main and not do_this_function_first? Probably to keep similarities to earlier programming languages.

By convention, your program returns an integer value (the return value of main) to the shell to indicate whether the program completed successfully (a zero) or non-successfully (non-zero, often related to an error code).

Mark Rushakoff
While it's the primary entry point, global and static initialization can cause other functions to be called prior to main (i.e. `int x = foo();` outside of the function will cause mean `foo()` will be executed prior to `main()`).
R Samuel Klatchko
@R Samuel I didn't know that. Interesting, thanks!
Ninefingers
@R Samuel: That is true, however, in some operating systems (especially ones that don't have `.init`/`.fini` section support), compilers are basically required to put all the `.init` code into the top of `main` instead (and register `.fini` code with `atexit`). Hence, it's very important for `main` to be compiled with a C++ compiler (even though `main` usually has no name mangling done to it, unlike most other C++ functions), even if all the other C-like functions used by the program can instead be compiled with a C compiler.
Chris Jester-Young
@ChrisJester-Young - thanks, that's an interesting portability issue to know about.
R Samuel Klatchko
A: 

Main is the entry point in to you program, for a c or c++ program to run you must have this Main method as the point for your program to start executing from. It also takes in command line arguments you wish to pass in. more info here:
http://en.wikipedia.org/wiki/Main_function_%28programming%29#C_and_C.2B.2B

Craig
+1  A: 

From the C++ Primer, 4th edition:

"Every C++ program contains one or more functions, one of which must be named main. A function consists of a sequence of statements that perform the work of the function. The operating system executes a program by calling the function named main. That function executes its constituent statements and returns a value to the operating system. The operating system uses the value returned by main to determine whether the program succeeded or failed. A return value of 0 indicates success. The main function is special in various ways, the most important of which are that the function must exist in every C++ program and it is the (only) function that the operating system explicitly calls. We define main the same way we define other functions. A function definition specifies four elements: the return type, the function name, a (possibly empty) parameter list enclosed in parentheses, and the function body. The main function may have only a restricted set of parameters.[...] The main function is required to have a return type of int, which is the type that represents integers. The int type is a built-in type, which means that the type is defined by the language."


Related to the meaning of int main is the following entry in Bjarne Stroustrup's C++ Style and Technique FAQ:

Can I write "void main()"?

which also quotes the ISO C++ standard 3.6.12.


Finally, there have been in the past two other questions on stackoverflow that are very related:

why do we give int main in c++ and not void main?

and

Difference between void main and int main?

Alexandros Gezerlis
A: 

I'll add to this, not in "answer" as such but some more technical info. "main" is indeed the startup routine for most C common runtimes, and C++ and as such Java has static void Main() too, for no other reason than history. What usually happens is the entry point is elsewhere in the executable and main is called later after the appropriate libraries are loaded in for the platform.

That said, there are alternatives. Two frequent ones appear on Windows:

  • WinMain or WMain is the main routine used by the Windows API for GUI applications. You'll see this, too, with MinGW as well as Visual Studio, using the option from LINK.EXE /SUBSYSTEM:WINDOWS.
  • If you were to use /SUBSYSTEM:NATIVE you'd generate a Windows "Native App", that is to say, Native to the kernel and not to the Win32 subsystem (read up on that if you're interested - the stuff going on behind the Windows launch screen and programs like chkdisk are examples on WinNT+ platforms). These processes have an entry point at NtProcessStartup(), totally different to the main you'd expect.
  • DLL EntryPoints on Windows are called DllMain, see http://msdn.microsoft.com/en-us/library/ms682596%28VS.85%29.aspx.
  • Linux Kernel Module entry points are called init_module.

What I'm trying to say is that main is the standard name for an entry point but not by any means the only one - many APIs work differently, some with good reason, others not, and not all (in fact all of the above) don't conform to the usual int main(int argc, char** argv) definition. "main" in the sense of the the usual option plus all of the above is simply "where your code starts", or if you want to talk at a system level, the address at which to start executing.

Hope that gives you some background info in case you see any alternatives.

Ninefingers