+ 1
Shouldn't "int main()" return an integer ?
I guess my question is very basic but I will really appreciate any help: In the section "Creating a class" we see the code int main() { BankAccount test; test.sayHi(); } Which works. But I don't understand why this function, which is not void and is supposed to return an integer, has not the part of "return 0;" and in fact returns a string ("Hi") with no error. Shouldn't there be an error as the output is not an integer? Many thanks
6 Respuestas
+ 6
Your code will run for some weird reason, it should return an integer to know when the program ends, or stops. My guess is that in that case, without the return 0; program runs, but without ending, until you click away or close.
+ 2
The main function is an exception to the rule.
You are right, it should return an integer, but the C++ standard says that if the main function doesn't return anything, this is the same as returning 0.
+ 1
Thanks all for your help and specially to ~Designing for the detailed answer!
0
if we define int main() instead of void main() then we have to return 0 at the end of the program. But it not in the case of void main()
- 1
It depends on the environment. Some newer IDEs will add the return statement for you in main, older will give you an error