0

The question is regarding c return 0

#include<studio.h> int main { some code. return 0 } return 0 is true or false? if the code execution is successful then the control will go to Reuter 0 or not?

22nd Jul 2017, 5:57 AM
punith p
punith p - avatar
9 Answers
+ 9
Return - returns a value to the function int aFunction() { return 5; // return the value 5 } int main() { cout << aFunction(); // will print 5; return 0;// program ran fine. Tell the operating system everything went fine }
22nd Jul 2017, 6:12 AM
jay
jay - avatar
+ 6
return 1 tells the operating system something went wrong. anything other than 0 means something went wrong. Not sure the values have any special meaning. Apoorva explains all this well
22nd Jul 2017, 6:16 AM
jay
jay - avatar
+ 4
@Punith. I think you are getting confused with goto.
22nd Jul 2017, 6:08 AM
jay
jay - avatar
+ 3
When you return 0 or return EXIT_SUCCESS, you tell the caller (OS in case of main) that the status of your program was successful and it could do what it was supposed to do. Returning some other values may indicate an error. It can be useful when you write some script that calls multiple programs. And depending on the success of one application, you want to take a decision in your script. One less common use (hack) is to check the return value when main is called recursively. Do note that calling main function recursively is prohibited in C++. So calling main from your program may give unexpected results in C++. Also note that on C++ compilers and C99 onwards compilers if you skip return statement in your main function, compiler would add a return 0 or return EXIT_SUCCESS for you.
22nd Jul 2017, 5:59 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
+ 3
As you say, main() is declared as int main(). The OS expects an integer back, so it knows what to do next, especially if another program or script invoked your program. 0 means "no error." Anything else means an error occurred.
22nd Jul 2017, 6:22 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
+ 1
thank jay
22nd Jul 2017, 6:16 AM
punith p
punith p - avatar
+ 1
oh thanks
22nd Jul 2017, 6:23 AM
punith p
punith p - avatar
0
what is the use of return 0 in a program?
22nd Jul 2017, 6:10 AM
punith p
punith p - avatar
0
return 1 in main program
22nd Jul 2017, 6:13 AM
punith p
punith p - avatar