0
#include <stdio.h>
int func()
{

}
int main(void)
{
    printf("%d\n",func());
    return 0;
}

the function "func()" is of "int" return type but is not returning anything. When the function is called in the print function, why is it giving an output 0? And why does it compile successfully although the function definition does not agree to the function code?

b4hand
  • 9,550
  • 4
  • 44
  • 49
Aman Mahajan
  • 153
  • 1
  • 10

1 Answers1

4

If you enable warnings you will see a diagnostic. Traditionally, C implicitly allowed all functions to return int. The behavior of the return value is undefined, so it is not guaranteed to be 0. The behavior could change on different compilers or on different platforms or with different optimization flags or even just by adding or removing other unrelated code. The reason you are probably seeing 0 is because you are running unoptimized and whatever previous value happens to be in the register or stack position is 0. This is pure chance, and relying on the behavior will ultimately result in bugs in your code.

b4hand
  • 9,550
  • 4
  • 44
  • 49