i have a question that.

is it not possible that main do not return any value, and the returning value of main is void, why returning value of main should always be int,

when we write

void main()
{

}

mostly people here on DANIWEB ask to write int before the main, i.e

int main()
{

return 0;
}

is it not a right way to write void before main. if not then please tell me the reason? that why returning value of main should not be void...


as in reference you can see in the post : http://www.daniweb.com/forums/thread11802.html

that Narue has written "
void main()
main does not, and never has, had a return type of void. The return type of main is int, and anything else is wrong."

Edited 5 Years Ago by AmerJamil: n/a

jonsca@ if i asked this question here, then whats the problem answering over here? on Google i mostly saw programs of books that has written void as well

thats is why i had a confusion because one of your member in older post told that returning value of main is alway int ,main never has no returning value of void.
so thats why i asked it over here.

It is always int, according to the standard.

Again, I wasn't trying to be so harsh, but typing in "int main versus void main" in google popped that answer up right away. It's great to ask questions and I'm by no means discouraging you from doing that, but google it first and see if you get an immediate answer.

that Narue has written

She's darn near always right!

it is a big article that you sent, can you please tell me short and best answer so that i can understand it more clearly...

my teacher also uses void main(), when i told him that on internet i got that main() always return integer value, and it is standard.

my teacher asked me the reason that why it is standard to use int before main?

and i have no appropriate answer and reason to give him that main always return integer value and it cant be void.


compilers we are using:
Borland

can you please tell me short and best answer so that i can understand it more clearly...

Compilers are not required to support anything but the following two definitions of main (equivalent definitions are allowed though):

int main()
{
    ...
}
int main(int argc, char *argv[])
{
    ...
}

The problem with void main is portability. Your code might be just peachy on one compiler, but invoke undefined behavior on another. There's no reason to introduce that kind of risk on a trivial feature that doesn't do anything or save you any effort. In fact, in standard C++, void main actually adds a single keystroke because you can always omit the return statement.

my teacher asked me the reason that why it is standard to use int before main?

Now that's a good question. Usually the question is "why do we have to return int", and the answer is "because the standard says so". But why the standard says so is a more interesting matter of history. In terms of C++, Stroustrup's desire to maintain as much compatibility with C as possible had a huge influence. The return value and multiple version rules of C were kept for C++. So for C++ the answer of "why" is simply, "because that's how C did it at the time". Now why did C do it that way?

Way back when, C was designed for and ran on UNIX. UNIX had a runtime startup that called main with two arguments: argc and argv. Further, termination of main was performed with the equivalent of exit(main(argc, argv)); . The return value was used as a way to communicate exit values to the runtime environment. Now it gets interesting. UNIX added environment variables and main was updated to support them with a third argument, envp, but because environment variables were also available from the getenv function, common practice was to continue defining main with two arguments (ie. many programmers lied to the compiler by providing a different definition than expected).

When C started to gain popularity and portability became more of a concern, it was decided that the portable definition of main would take two arguments (argc and argv), ignoring the envp parameter because nobody really cared about it, and return int as an exit value for the runtime environment. It looked like this:

int main(argc, argv)
    int argc;
    char **argv
{
    ...
}

Why was void never considered at this point for the return value? Because C didn't even have the void type yet! But it gets better, because K&R 1 was released around this time and littered with code like this:

main()
{
    ...
}

There are two significant things to note in that example:

  1. Due to the implicit int rule (which was removed in C99), omitting the return type still means int, but it's less obvious. An uneducated reader would think "oh, no return value".
  2. There are no parameters.

This introduced a precedent where there were two common definitions of main: one with two arguments and one with none. This was a problem for the standards committee, which was given the task of bringing together all of the slightly different dialects of C into one common language. The question here is, of course, which definition of main to bless as standard.

The committee (mistakenly, in my opinion) chose to support everything, and specifically bless the two argument and zero argument versions, both returning int. There's a clause in the standard that specifically allows an implementation to support anything under the sun, provided all of the options are documented (the so-called "implementation-dependent" feature). Thus, main became a "special" function with multiple supported definitions.

The blessed return type was int simply due to prior art. Returning int was vastly more common than void. In fact, void main at the time was pretty much limited to a handful of C programs written on the PC with implementations that supported it. Of course, the benefit of communicating with the runtime environment using an exit code couldn't be ignored either. We're talking about a time when programs were run from a command line rather than a GUI, so exit codes were far more valuable than they are today.

Once again, C++ follows suit and adopts the rules of C, for the most part. Really the only difference between C and C++ in terms of main is the ability to omit a return statement in C++ and main automatically returning 0 (a feature which was added to C99).

Cliff's Notes:

  • C++ requires int main because C requires int main
  • C requires int main because void wasn't introduced until well into C's evolution
  • Standard C only explicitly allows int main because void main wasn't common enough at the time that consider as an exception

Edited 5 Years Ago by Narue: n/a

Comments
Thank you
Nice explanation
very interesting!!
I agree with AD. If you wrote a book, I would buy imediately. I sometimes read your posts just to learn something new. Thank you.
When are you going to write your own book :)
Well written complete answer.
This is getting a bookmark (you've doubtlessly said it before, I know)
Succinctly, comprehensively and very patiently put!

If you have read about functions then you must be knowing that the general syntax of function looks like:
Return type-function name(parameter list)

The significance of return statement is that it returns value to the calling code..
So who calls the main function? Your Operating System.

So when we use int main we use a statement return 0; This means that main function returns an integer value to the Operation System. Return 0 signifies that if the program executes successfully, main functions returns value 0 to the OS telling it that everything went perfect.

So if we are using void main which means not returning any value then the OS doesn't know where to exit the program and whether our program worked correctly or not.

Nice necropost there Mrigank.

But if it's already up to see the sun.
We use use int before main() {} because program that malfunctions returns error code other than 0. A program that is successful (a.k.a. ended it's job), return 0;, rather than returning string that is overly compilcated, or boolean that is extremely limited, we use integers to indicate if the job succeded. Errors are then cryptic enough to not be read by user, cryptic enough to be able to be fixed only by developers.
That's my 5 cents.

Edited 3 Days Ago by Aeonix