0

Hi all,

1: #include <iostream.h>
2: 
3: int main()
4: {
5: cout << "Hello World!" << endl;
6: return 0;
7: }

In the above code does the line 6 (return 0), required by the operating system,that means tells the operating system a program finish?

If true, what the useful of close brace main function (line 7)?

Thanks

3
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by Ancient Dragon
0

>>that means tells the operating system a program finish?
No. There is other code that you did not write that does that. main() returns not to the os but to the start-up code that called main(). What that startup code does is not specified by standards and is therefore different for every compiler.

Every function must have an opening brace { and a closing brace }. All functions which are not declared void must return a value, and main() is no exception. The return value of main() is not actually used the the os, but the os may pass it on to another program which spawned it, or a shell script (batch file) might use it for something. The return value of 0 typically means successful completion, and any other value is an error number of some sort.

0

> In the above code does the line 6 (return 0),
There are 3 standard values

0 - which means success
EXIT_SUCCESS - which also means success
EXIT_FAILURE - which mean it didn't succeed.

You can also return any other value (in an implementation defined manner), though almost every system uses small positive integers in the range 0..255

0

Thanks alot MR. Ancient Dragon

But what do U mean by "the os may pass it on to another program which spawned it, or a shell script (batch file) might use it for something"

0

Thanks alot MR. Ancient Dragon

But what do U mean by "the os may pass it on to another program which spawned it, or a shell script (batch file) might use it for something"

Program #1 can start (or spawn) Program #2 then wait for Program #2 to finish. After it finishes the operating system will send Program #2's return value from main() back to Program #1, which it can then use for whatever purpose it wants to. The same can be done in batch files (MS-DOS or MS-Windows) or shell scripts from *nix.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.