Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 632 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why a function with returntype in c works with out return statement?

#1
> **Possible Duplicate:**
> [Why can you return from a non-void function without returning a value without producing a compiler error?](

[To see links please register here]

)

<!-- End of automatically inserted text -->

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

Reply

#2
You're causing undefined behaviour, so literally *any* output is possible.
Reply

#3
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 `return` is 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.
Reply

#4
Its just random junk in memory you're getting back, hence, each call in the second example produces different results
Reply

#5
it is Undefined behavior to get the return value of function which is declared to not return anything.

Undefined behavior means that anything is possible and the outcome of the operation is not defined by the C++ standard. You cannot estimate or approximate the outcome in any of the program runs.
Reply

#6
Technically, you are invoking "undefined behavior." Undefined behavior behaves, well, without definition. If the behavior of your program is undefined, it might print 512, 4, 99, or even the complete text of the Gettysburg Address.

I wouldn't describe the results you showed as 'working' in any reasonable sense of that word.

P.s. If you are using the GNU Compiler Collection, I recommend using *at least* these switches: "-Wall -Werror".

P.p.s. In years gone by, in the days before ANSI-standard C, a certain non-optimizing Z-80 C compiler would accept this program fragment:

int ReturnThree() {
3;
}

and produce the same code as if the user had specified:

int ReturnThree() {
return 3;
}
Reply

#7
Why is your question tagged C and C++ at the same time, while the title specifically refers to C? These two languages are significantly different in this regard.

In C language such functions "work" because the language requires them to work. Why not? The function that is declared with some return type might still do some useful work (besides returning anything). So, not having a `return` statement in such a function is perfectly fine from the formal point of view. It is not a good programming practice though. The function will work as long as you are not trying to use the returned value (which the function didn't really return). You are trying to use that value, which makes the behavior of your code undefined. You are claiming that it "works". In reality it doesn't. What happens in this case and why - these questions have no answers. For all means and purposes your code's behavior is essentially random and unpredictable.

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



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through