+ 8
Why we use return=0?
17 Antworten
+ 12
this method to help you track your errors for example:
#include <iostream.h>
using namespace std;
int main()
{
int temp=0;
cout << "Enter a number bigger then 1: " << endl;
cin >> temp;
if(temp <1)
return -1;
cout << "The number is: " << temp << endl;
return 0;
}
if you input a number smaller then 1 then porgram stop and in the output window of your IDE will print something like:
threat 0xffffffff exit with code -1;
in the end it's an old way to track errors but it very useful in more complicated codes.
i use it personally ;).
hope that help
+ 3
It is only used when you declare your main function as int like
int main()
{
//here is your code
return 0;
}
Try void instead of int you don't need to use return 0. But you have to use getch(); else your program console will close it automatically(works only in pervious version of c compiler). Return 0 also used to hold program console to stop automatically closing. When you enter some keyword it close its console.
+ 2
Becuse you let function main with int return, so the function shold return value.
If you want not, let function main with void return.
ex:
void main (){}
+ 1
we use return 0 because the main function is an int function and it returns any integer, for no specific return value, we write as code "return 0".
+ 1
this is long rooted and comes stems from the UNIX operating system, where the adagio is "no news is good news". A return code of 0 means all-OK, thumbs up, in the clear... you get what I mean.
0
Return statement is used as in c++ main is rwturning int as int main () shows........whereas complier predict return=0 that program has terminated normally whereas if we return non zero integer then compiler predict it as abnormally termination of code!!
0
Modern compilers will invisibly add the return 0 statement for you if you leave it out, so you don't need to write it unless you have a use for it.
0
it is not just a value, return value of main() is a code understood by operating system.0 is for success and nonzero is for failures
0
this is my understanding, although Im still learning :)
we return a value of 0 to indicate that the function executed as expected, without any problems. Any nonzero char will tell the compiler that an error has been encountered during execution.
if we use type int for our main function
I.e.
int main()
{
... whatever statements ;
return 0;
}
this indicates that our main function is of type int, therefore we can only return an int from it. should no return value be specified, then a 'trash' value will be returned that we can usually ignore.
return 0 simply ensures that a 0 is returned rather than any other int when the function completes. if you use void (usually it will generate an error), therefore int with return 0 is the standard.
0
to end your source code
0
its the end or closure of every program
0
clean compilation
- 1
so if i use void instead of int i don't have to use return
- 1
if anyone use another one try bcz zro is only use fr return if other one try it and told me👌
- 1
To sum it up, if you declare your main as int main () then main acts like a function and requires a return value. Most people use 0 as the return value because typically that denotes successful completion to whatever is calling the program. If you declare your main as void main () then main is a sub routine and not a function, and as such does not have a return statement.
- 2
we use it as 0 has no value!
- 2
return 0 simplify no errors