c - What will a function return if there's no explicit 'return' -
this question has answer here:
i bumped problem when forgot write return clause of function, there no warning or error in gcc. fixed started wondering why function return meaningless without return. here examples tried:
#include "stdio.h" #include "stdlib.h" int func1 () { int i; = 2; } int func2 (int a) { int = a+3; } int func3 () { int i; (i = 0; <= 1; i++); } int main(void) { int = 0; int b = 0; int c = 0; = func1(); printf("a = %d \n", a); b = func2(a); printf("b = %d \n", b); c = func3(); printf("c = %d \n", c); } and results are:
a = 1 b = 4 c = 7 my questions:
1) why these results? there general rule this?
2) why keep thing rather report error? can somehow 'useful' somewhere?
this undefined behavior , depend on calling convention being used. if caller expecting result in register whatever value last in register used.
edit
the draft c99 standard in section 6.9.1 function definitions paragraph 12 says:
if } terminates function reached, , value of function call used caller, behavior undefined.
clang warn default , gcc warn -wall, in general should enable warnings.
Comments
Post a Comment