+ 2

Return 0

Hi everyone, I can put an another value than 0 in "return 0" function and there is no error while the learning mode explained that the 0 means the program was successfully executed and any another value means an error or something like that ? Thanks you in advance!

2nd Apr 2019, 5:33 PM
UlysseP
2 Answers
+ 10
The return value from main only matters to the caller, the program itself doesn't care about it. The caller can use the return value to figure out what happened and can do subsequent actions depending on it. Handy for automated systems that call various executables and branches depending on the results. On windows the return value of the last executed program is stored inside the variable errorlevel. For example in the command prompt: g++ foo.cpp -o foo.exe echo %errorlevel% If it prints 0, compilation is succesful. If it prints 1, compilation has failed. The operating system/program doesn't blow up if I, the caller, don't care about it either. Also the error values can differ depending on the program, should be in the documentation somewhere if it does.
2nd Apr 2019, 5:48 PM
Dennis
Dennis - avatar
+ 1
Thank you a lot for this great explanation!
2nd Apr 2019, 6:30 PM
UlysseP