+ 1

Even if I don't add a 'return' statement, the program still runs and gives same output. Can anyone pl explain the difference?

'Return' statement: Even if I omit the return statement altogether or put 'return 1', 'return 2' I'm not able to cite the difference. Please explain what purpose does this serve while using int main ().

2nd Nov 2016, 7:37 AM
Samyak
Samyak - avatar
2 Réponses
+ 2
C/C++/whatever programs are typically run from the command line - the number you return is a way of telling the operating system (or other programs) whether everything has gone well or whether there was an error. If you return 0, that means success, anything else is failure. If you know your way around the windows console, you may have encountered 'dir', which lists all files in a foder: C:\Users\schindlabua> dir Desktop Volume in drive C has no label. Volume Serial Number is ABCD-EFGH Directory of C:\Users\schindlabua\Desktop 31/10/2016 15:37 <DIR> . 31/10/2016 15:37 <DIR> .. 26/09/2016 21:25 47,568 dank_memes.jpg .... If you now check the error code returned by dir, you'll find that everything has gone well. C:\Users\schindlabua> echo %ERRORLEVEL% 0 perfect! What happens if you try to dir some folder that doesn't exist? C:\Users\schindlabua> dir ouch Volume in drive C has no label. Volume Serial Number is ABCD-EFGH Directory of C:\Users\schindlabua File Not Found C:\Users\schindlabua> echo %ERRORLEVEL% 1 The errorlevel is 1, as expected! You can return any number you want from your programs, but it's best to stick to this convention. By the way, if you are programming in C or C++, you can omit the 'return 0;' inside your main function and the compiler will assume you meant that.
2nd Nov 2016, 8:33 AM
Schindlabua
Schindlabua - avatar
+ 1
What language are you using? Do you have a full code example?
2nd Nov 2016, 7:42 AM
Bart Genuit
Bart Genuit - avatar