return 0;



Hi,

Why some compilers do not complain about missing the return 0 statement ?
well some compilers seem to have a default 0 value, to when a function declares its return type as int and does not actually have a return statement.
well some compilers seem to have a default 0 value
Not true.

A C++ compiler should report an error.
well atleast one - used by dev c++ does not - and uses 0 as default value.

when tried :

int foo()
{
//whatever... - no return statement.
}

int main()
{
int b=5;
b=foo();
cout<<b<<endl;
}

no errors, and output is 0.

i know dev is not that good of an example but still..


this is just a result of some testing, i dont really have an answer i am sure of, so i'll just say i dont want anyone getting it wrong because of me.. these are just conclusions i made after trying the above code on a c++ compiler.
Last edited on
You get whatever's in EAX at the time in this instance. There is no default.
closed account (D80DSL3A)
There is an exception for main() permitting omission of the return statement.
I found this at http://en.wikipedia.org/wiki/Main_function_%28programming%29

From this article:
In case a return value is not defined by the programmer, an implicit return 0; at the end of the main() function is inserted by the compiler; this behavior is required by the C++ standard.

Thanks all :)
Topic archived. No new replies allowed.