This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What should main() return in C/C++?
why do we give int main in c++ and not void main?
I've started learning C++ and the following question came up to my mind: main() always return int? Cannot I declare void main() instead of int main()?
Thanks you!
Yes, main() must return int. The return value is passed back to the operating system, to indicate whether the program ran successfully: zero means success.
However, you can leave the return statement out of main (and only main) if you like; in that case, it will return zero.
It has to return an integer value. The returned value tells the computer what, if any, error codes there were. Returning 0 will tell it that there were no errors in the program.
Related
This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
What happens when a function that returns an object ends without a return statement
(3 answers)
Closed 4 years ago.
In a test, one of my classmates wrote the following function, to flip a number:
int tukor(int n)
{
int k=0;
while(n!=0)
{
k=k*10+n%10;
n=n/10;
}
n=k;
}
You will notice a complete lack of any return statements, but when cout<<tukor(1234); is run (namespaaace std is used), it outputs 4321. Now the entire class is confused as to how this is possible, even after the teacher added the lines n=0;n=12; at the end of the function. It has worked for all test cases so far.
Is this caused by undefined behavior or something similar?
EDIT: changing k before the n=k statement changes the return value, and if it is removed, the return value is 0.
This question already has answers here:
C++ return value without return statement
(6 answers)
Closed 6 years ago.
I am confused with the following output in C++
int add()
{
int c = 2+3;
}
int main()
{
int x = add();
cout << x;
return 0;
}
This prints 5.even if we do not write return statement.
How this is managed in C++.
Please help.
This is UB. You're right to be confused - this can work one day and fail the next. Don't rely on undefined behavior.
If you want to know why it works, it's because parameters & return values are passed on a data structure called stack (well - usually; sometimes passed in the same register). Similarly, most implementations use this same stack for locals. Therefore, the int in add will be located in the same place as where the return value is expected (by your specific implementation) and your implementation doesn't invalidate memory when your int there is destructed. But it's still destructed, it's still UB and it might break in any second.
As the comments wrote, you might turn on warnings to avoid this kind of error.
This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 6 years ago.
This function returns a value even when it shouldn't.
#include<iostream>
using namespace std;
int foo(int a,int b)
{
if(a>b)
return a;
else if(a<b)
return b;
}
int main()
{
int x=7,y=7;
cout<<foo(x,y);
return 0;
}
The output is:
7
Also it produces proper output only on a GCC compiler (I used Dev C++). Turbo C produced garbage value.
Can someone explain how this happens?
The behaviour on not returning a value on all program control paths is undefined.
The compiler is allowed to do anything.
Didn't your compiler warn you of this? (GCC ought too, Turbo C possibly not on account of its age).
This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 9 years ago.
When writing a C/C++ program, specifically with latest compilers, why do we need to return an integer from the main() method? Like int main() and we return "return 0" from it. So what is the exact reason behind this?
The return value of main() becomes the exit status of the process. Traditionally, an exit status of zero usually means “OK,” while any non-zero value indicates some kind of error. This is analogous with how many system calls likewise return zero or an error code.
Even more information at J. Leffler's epic answer to this, similar question: What should main() return in C and C++?
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.
It returns the 0 to OS to tell the OS that your program executed successfully.
The int value returned by main(), if any, is the program's return value to 'the system'. A zero value from main() indicates success. A nonzero value indicates failure. If no value is returned, the system will receive a value indicating successful completion.
See Why default return value of main is 0 and not EXIT_SUCCESS?.
Returning zero from main() does essentially the same as what you're
asking. Returning zero from main() does not have to return zero to the
host environment.
From the C90/C99/C++98 standard document:
If the value of status is zero or EXIT_SUCCESS, an
implementation-defined form of the status successful termination is
returned.
In other words, the specific value indicates success.
This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 9 years ago.
In most cases int main() does not return anything, it doesn't even have to since no return would not give an error. So why does main have to return an int? Why is void main not possible?
EDIT: I meant, why is int main() the standard if there usually is no return?
Other programs may use the return code to determine whether the application executed successfully. Zero typically means successful execution.
void is possible but non-standard. The returned int is meant to signify something for the caller.
The same way as a function in your program may returns values to indicate its result, main returns to indicate the result of execution of your program.
Also in case int main() does return explicit, compiler will put return 0 automatically