![]() |
return value from main
check the following program
/*a.c*/ int main() { return 0; } returns 0 to the shell and echo $? gives 0 /*b.c*/ void main() { } echo $? in shell gives some different values for each execution what does it represents???? |
Re: return value from main
Garbage values
|
Re: return value from main
It represents the fact that you haven't returned a value. The values are meaningless; you should not try to use them if the program doesn't return a value. But you should always return zero from main unless you have a good reason for returning some other specific value; zero indicates success, and for any script calling your program and relying on the return codes to check for errors a garbage return will cause undefined behaviour within the script.
If you know for certain that your program will NEVER EVER be called by a script that checks return values, and that returning garbage will definitely have no negative effects, then go ahead and return garbage. Otherwise at least return 0; it's not a difficult line to add. Or return EXIT_SUCCESS; if you prefer. |
Re: return value from main
Always define main as returning an int, then return one. It's that simple.
|
Re: return value from main
Quote:
After long time and so welcome back. |
Re: return value from main
The current C++ standard mandates that the main() function return int. However you don't have to ACTUALLY return a value. If the control flows out of main without reaching a return statement, it is equivalent to return 0;
On the other hand, void main() is ill-formed |
| All times are GMT +5.5. The time now is 05:02. |