main() return 1;

Posts 1–7 of 7 · Page 1 of 1
main() return 1;
I think the title pretty much says it all, it seems my book seemed to skip one little detail. It explained that main returns normally with a return of zero. It never specified what happens when main returns a one. Instead the author just started returning 1 like I new what it meant and from what it looks like a return of one seems to accomplish the same thing as a continue or something like that.
When passing a return value from a main method it is for the 'calling application' to pick this up much like you can do something with the return value of any function in your own application. Usually this calling application is the Operating System and your return value really just gets ignored. But if your application was started in a BAT file or by another application, that could then pick up that value and do something based on the value it receives.

return 0 usually indicates nothing happened (success) while any other number is up to the programmer to define. (1 = error in startup, 2=something went wrong in function a, 3=Out of memory, 4=user is an idiot etc etc )

Sorta goes against a normal 'true/false' bool return you may encounter in most programs and some people choose to follow that convention instead so they return '0' (False) or negative numbers if the program encountered an error and 1 (true) for success but the other way around is more common.
Quote Originally Posted by B1ackAnge1 View Post
When passing a return value from a main method it is for the 'calling application' to pick this up much like you can do something with the return value of any function in your own application. Usually this calling application is the Operating System and your return value really just gets ignored. But if your application was started in a BAT file or by another application, that could then pick up that value and do something based on the value it receives.

return 0 usually indicates nothing happened (success) while any other number is up to the programmer to define. (1 = error in startup, 2=something went wrong in function a, 3=Out of memory, 4=user is an idiot etc etc )

Sorta goes against a normal 'true/false' bool return you may encounter in most programs and some people choose to follow that convention instead so they return '0' (False) or negative numbers if the program encountered an error and 1 (true) for success but the other way around is more common.

Ok. We were instructed to return one when a file couldn't be opened properly, and he never really explained why, but to say that any number other then zero was an error, so I assumed it was kind of a boolean thing. Thanks BA.


And HD stop spamming. Even I know you don't return 1337 on failure D:
Now in what case do I return a number over 9000?
Quote Originally Posted by why06 View Post
Ok. We were instructed to return one when a file couldn't be opened properly, and he never really explained why, but to say that any number other then zero was an error, so I assumed it was kind of a boolean thing. Thanks BA.


And HD stop spamming. Even I know you don't return 1337 on failure D:
Now in what case do I return a number over 9000?
ummm, you type return 9000; at the end?
Or return a variable with the name over9000.

I don't think windows cares as much what you return anymore. Unless you run it from command prompt.
Quote Originally Posted by why06 View Post
And HD stop spamming. Even I know you don't return 1337 on failure D:
Now in what case do I return a number over 9000?
i return whatever i want to return ;p its not used for anything anyway(as long as you dont call it from another application etc)
In which case do you return a number over 9000?
Whenever you want really.
Perfect example is the 'HRESULT' values returned by MS
Common HRESULT Values (Windows)

Besides S_OK being 0x00 which looks fairly normal, the rest is just
'out there' value wise and is all just decided by someone who designed
it and for whatever reason decided those would be the values.

Maybe they figured they'd have groups of errors/warnings like 1000-1999 could be generic warning messages 2000-2999 could be IO Errors etc etc (obviously with room to grow for future needs lol)
I always return 1337 on succes and a random number below 1337 on failure ;o
Posts 1–7 of 7 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us