Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   return value from main (http://www.go4expert.com/forums/return-value-main-t23166/)

abhishek.biradar 1Sep2010 12:20

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????

shabbir 1Sep2010 18:41

Re: return value from main
 
Garbage values

xpi0t0s 2Sep2010 11:29

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.

DaWei 2Sep2010 17:01

Re: return value from main
 
Always define main as returning an int, then return one. It's that simple.

shabbir 2Sep2010 17:38

Re: return value from main
 
Quote:

Originally Posted by DaWei (Post 72287)
Always define main as returning an int, then return one. It's that simple.

Offtopic comment:
After long time and so welcome back.

LordN3mrod 7Sep2010 00:52

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 06:20.