1

Possible Duplicate:
Function returns value without return statement

what is the return value of the function that returns int, but isn't returning anything explicitly.

For instance output of

 int fun() { printf("\ncrap"); } void main() { printf("\n return value of fun %d", fun()); } 

The output I am getting is :

 crap return value of fun 1 

Does this depend on compiler I am using ?

Thanks !!!

3
  • Which compiler are you using? Commented May 31, 2012 at 1:14
  • @Tom...i m using gcc version 4.6.1 Commented May 31, 2012 at 1:15
  • The answer is whatever's in the eax register, I had to add __asm { mov eax, eax; } at the end of the function in order to get it to compile on VC++2010. To get a value of 1 is strange as I would have thought it would have been the return value from the printf("\ncrap") call, which would have been 5. Commented May 31, 2012 at 1:24

3 Answers 3

2

You don't have a return value in fun() function, so the value returned is undefined.

Sign up to request clarification or add additional context in comments.

Comments

1

It might be returning whatever's in the EAX register, which is probably unreliable without an explicit return value:

https://stackoverflow.com/a/4644913/375399

Comments

-1

It depends on compiler. That's my output, for your information

compiler: i686-apple-darwin11-llvm-gcc-4.2

crap return value of fun 0 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.