Discussion Locked

Return value of main

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

In main function of C++ if you define the prototype of main as void main(void) then what value does the program returns to OS.

If it returns nothing then how OS will know whether the program executed succesfully or not

7 replies

by basilisk

It probably depends on the OS, but I would guess that if the programmer doesn't have the main function explicitly return anything, it returns a default value which the OS interprets as success.
Even a program with void main() can return a value though, through the use of system library functions like exit().

by jeng

The return value would be unknown, or undefined. There wouldn't be any method of knowing if the application completed or not (unless you write a log file).

From another point of view, ANSI doesn't approve of the prototype void main(void). main() must always return a value. In some OS, having a void main() can cause stack problems.

If your program doesn't need to return a value, you still must declare main as int main(void). Return an "EXIT_SUCCESS". Check your OS or C++ compiler for all the details.

by cratem

Dear Sankar,
an OS does not need any process or application to return values for it to know that the process ended. Return values are for the OS to pass to calling programs. The OS is running the program for you and it knows whether theprocess is running or even not responding. When the program terminates obviously the OS knows that it has terminated. Programs written in Visual Basic does not provide a return value. It is also not possible as far as I know to pass a value from a VB application to any other application. I had a need from my customer who wants a DLL of mine to pass a value after sucessfully doing a job. I had to make the DLL write the result to a file and a small c++ program to start a VB program to start the DLL and pass it to the calling application by reading the data put into a file by the VB program.

This discussion is closed to new replies.