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?
9 Réponses
+ 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
}
+ 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
+ 4
@Punith. I think you are getting confused with goto.
+ 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.
+ 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.
+ 1
thank jay
+ 1
oh thanks
0
what is the use of return 0 in a program?
0
return 1 in main program