[Solved] Why is this function returning 1?


So, does an int function return 1 by default?

No. There is no default return value (except for main, which returns 0 by default.)

It returns garbage value.

Correct. More correctly: The behaviour of the program is undefined.

But then why it is returning 1 here!!!

Because the behaviour is undefined.

I think you misunderstand what “garbage” value means. Garbage can have any value. 1 is within the set “any value”.

but it is returning 1 every time on every PC!!

Because the behaviour is undefined. The standard does not guarantee that the return value be other than 1 on any occasion. Considering that, why would you expect the function to not return 1? Of course, it doesn’t guarantee that the return value be 1 either, so expecting that would be equally silly. Any expectation would be folly when you know that the behaviour is undefined.

1

solved Why is this function returning 1?