Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

returning value from main()? 1

Status
Not open for further replies.

hughLg

Programmer
Feb 18, 2002
136
0
0
MY
According to all the book that I readed, the main() function should return integer value to the operating system to indicating its successfulness. But is there any way to capture this return value in Windows?
I've tried to get the return value from system() function when using it to invoke a program, but it always return 0.
 
hmmm....that is mostly accurate, but if I'm not mistaken, you can have a void main, and, as such, no return type...I can't say that I've actually used a void main function, but have heard of it. This could be the case, of the system function that you are waiting to return....

Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
Look CreateProcess() and GetExitCodeProcess().
 
hughLg said:
According to all the book that I readed, the main() function should return integer value to the operating system to indicating its successfulness. But is there any way to capture this return value in Windows?
I've tried to get the return value from system() function when using it to invoke a program, but it always return 0.
It is probably returning 0 because the program you call is returning 0. The system() function should return whatever value the program returns, but many programs don't use the return value for main and just return 0.

Try creating two simple programs. The first just returns a number other than 0:
Code:
// Prog1.cpp
int main()
{
    return 14;
}
The second just calls the first and prints out the number it returned:
Code:
// Prog2.cpp
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    int retval = system("Prog1.exe");
    cout << retval << endl;
}
If you run Prog2, you will get 14.


As a side note, void main is allowed by some compilers (like Microsoft's) but it is considered to be invalid C++. Since a return 0 is implied if you don't return anything, it is probably easier to just to use int main unless you are still using VC++ 6.0.
 
> It is probably returning 0 because the program you call is returning 0.

I'd believe, that system() returns 0, because the process, started by system() is not ended yet and there is nothing to return at the moment.
 
MSDN said:
Return Value

If command is NULL and the command interpreter is found, returns a nonzero value. If the command interpreter is not found, returns 0 and sets errno to ENOENT. If command is not NULL, system returns the value that is returned by the command interpreter. It returns the value 0 only if the command interpreter returns the value 0. A return value of – 1 indicates an error, and errno is set to one of the following values:
The status you get back is the status of the command interpreter, not the status of the command you executed (unfortunately).
When you do this
Code:
system("myprog.exe");
What really happens is more like
Code:
system("cmd.exe /c myprog.exe");
The command interpreter gets the return status of your program alright, but then TOTALLY fails to pass that back to you in its own return status. It's like the command interpreter saying "yes, I successfully ran the program, so now I'll return 0 to indicate that success".

other options include exec() and spawn(), but these need a bit more effort to use them, but you do get better control over what actually happens.
As would the using the routines previously mentioned by mingis.

Of course, if your OS isn't windows, then system() usually does the right thing in returning the exit status of the actual command.

It should be noted that the only defined return result for system is when you do [tt]system(NULL);[/tt] to determine the existence of a command interpreter. Everything else about the system() call is implementation dependent.

--
 
Salem, are you sure that means what you think it means? When I compiled and ran the code I posted above on Windows XP using VC++.NET 2003 and VC++6.0 I got the correct return value.
 
Yeah, ignore me on this one - it works for me too :)


--
 
> if I'm not mistaken, you can have a void main

From (written by Bjarne Stroustrup, creator of C++; so he probably knows what he's talking about):


The definition
Code:
void main() { /* ... */ }

is not and never has been C++, nor has it even been C. See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1. A conforming implementation accepts
Code:
int main() { /* ... */ }

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

A conforming implementation may provide more versions of main(), but they must all have return type int. The int returned by main() is a way for a program to return a value to "the system" that invokes it. On systems that doesn't provide such a facility the return value is ignored, but that doesn't make "void main()" legal C++ or legal C. Even if your compiler accepts "void main()" avoid it, or risk being considered ignorant by C and C++ programmers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top