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 !!!

Community
  • 1
  • 1
Kundan Kumar
  • 1,974
  • 7
  • 32
  • 54
  • Which compiler are you using? – Tom May 31 '12 at 01:14
  • @Tom...i m using gcc version 4.6.1 – Kundan Kumar May 31 '12 at 01: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. – James May 31 '12 at 01:24

3 Answers3

2

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

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
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

Community
  • 1
  • 1
Nick
  • 4,901
  • 40
  • 61
-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
waitingkuo
  • 89,478
  • 28
  • 112
  • 118