Possible Duplicate:
Why can you return from a non-void function without returning a value without producing a compiler error?
void main()
{
int y=0;
clrscr();
printf("%d\n",Testing());
y=Testing();
printf("%d",y);
getch();
}
int Testing()
{
int x=100;
//return x;
}
Result
512
4
i am not returning any thing from the testing function still value is coming?
Another Question also
void main()
{
char Testing();
int y=0;
clrscr();
printf("%d\n",Testing());
if(Testing())
printf("if--exi");
else
printf("else--exi");
getch();
}
char Testing()
{
char y;
//return y;
}
Result
0
if--exi
if printf is commented then result is
else--exi
why is it happening like that
A function declared to return a value that reaches a path without a return has undefined behavior. You just got unlucky that it appeared to still be returning a semi-plausible value. The compiler assumes that a
returnis called in all exits from the function and that a return value will be available at a particular location.In your second example effectively anything can happen, including returning different values each call.
g++ has a warning to detect this problem, and it’s highly worth enabling.