In c the return value for main() is optional why in c++ the main() must return value ?

Showing Answers 1 - 12 of 12 Answers

akshay

  • Dec 6th, 2006
 

Because in C we use void main and void is also default but in C++ we generally use int and int is also default, hence the difference

from swixs_akshay@yahoo.com

  Was this answer useful?  Yes

Raagu

  • Dec 8th, 2006
 

In C or C++ for main() if there is no return value we get only warning error and nothing else.

  Was this answer useful?  Yes

abcdef

  • Dec 19th, 2006
 

In c every function by default returns an integer value. By using void we mean a value zero is return to the environment i.e., operating system.

From the C standard:

------------------------------
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 the are local to the function in which they are declared):

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

or equivalent; 9) or in some other implementation-defined manner.

...

9) Thus, int can be replaced by a typedef name defined as int, or the type of argv can  be written as char ** argv, and so on.
----------------------------

Unless your compiler documentation explicitly says that "void main()" is a legal signature for main(), then the return value is not optional.

Typing main() to return void is not the same as returning 0 to the execution environment.  Strictly speaking, the behavior is undefined, and the result could be anything from the program running with no apparent problems to a crash on exit to failure to load at all. 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions