Why does the function with returntype in c work with the output of return?

Possible duplicate:
Why can you return from a non-void function without returning a value without 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 

Am I not returning anything from the test function still the value?

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 out then the result

 else--exi 

why it happens?

+7
source share
6 answers

A function declared to return a value that reaches a path without returning has undefined behavior. You were just unlucky that it seemed to still be returning semi-credible value. The compiler assumes that a return is called in all exits from the function and that the return value will be available in a specific place.

In your second example, anything can actually happen, including returning different values ​​for each call.

g ++ has a warning to detect this problem, and it is very convenient.

+5
source

Why is your question labeled C and C ++ at the same time, while the title specifically refers to C? In this regard, the two languages ​​differ significantly.

In C, such functions "work" because the language requires work from them. Why not? A function declared with some type of return value may still do some useful work (besides returning something). Thus, the absence of a return expression in such a function is different from the formal point of view. However, this is not good programming practice. The function will work until you try to use the return value (which the function really did not return). You are trying to use this value, which makes the behavior of your code undefined. You claim that it "works." This is actually not the case. What happens in this case and why - there are no answers to these questions. For all means and purposes, the behavior of your code is essentially random and unpredictable.

In C ++ functions that "forget" to return a value using the return , they always immediately lead to undefined behavior, regardless of whether the value is used by the caller or not.

+3
source

Its just a random garbage in memory that you return, therefore, each call in the second example gives different results.

+2
source

this is Undefined behavior to get the return value of a function that is declared as not returning anything.

Undefined behavior means that everything is possible, and the result of the operation is not defined by the C ++ standard. You cannot evaluate or approach the result in any of the program runs.

+2
source

You invoke undefined behavior, so any output is literally possible.

+1
source

From a technical point of view, you invoke "undefined behavior". Undefined behavior behaves, well, without definition. If the behavior of your program is undefined, it can print 512, 4, 99, or even the full text of the Gettysburg address.

I would not describe the results that you showed as "working" in the reasonable sense of the word.

Ps If you use the GNU compiler collection, I recommend using at least these switches: "-Wall -Werror".

Pps Over the years, a few days before the ANSI C standard, some non-optimizing Z-80 C compiler would accept this piece of code:

 int ReturnThree() { 3; } 

and enter the same code as the user:

 int ReturnThree() { return 3; } 
0
source

All Articles