# C: default return value if no value is given?



## kpa (Jun 15, 2012)

So what do you think this program should print?


```
#include <stdio.h>

static int
testfunc(void)
{
    return;
}

int
main(int argc, char *argv[])
{
    int a = 100;

    a = testfunc();

    printf("%d\n", a);

    return (0);

}
```

For me it prints -1 but that just doesn't make any sense, is this behaviour actually documented anywhere? This code compiles with the base system gcc(1) but clang(1) rejects it outright because a non-void function returns no value.

I came across this when trying to fix sysutils/ldapvi to compile with clang(1), there's one line where a function returning an int just returns without a value.


----------



## Uniballer (Jun 15, 2012)

The behavior is undefined by the C language, because you didn't do in the function what you said you were going to do in the function declaration.  In practice, on x86 the value of the function will be whatever happens to be in register eax on return.

I compiled your code with option 
	
	



```
-Wall
```
 and the compiler (gcc) said, 
	
	



```
junk.c: In function 'testfunc':
junk.c:6: warning: 'return' with no value, in function returning non-void
```


----------



## kpa (Jun 15, 2012)

Well that means that sysutils/ldapvi is depending on undefined behaviour as I suspected, on the line that clang(1) barfed on the return value that is undefined is added to a counter variable. To the trash bin...


----------



## Uniballer (Jun 15, 2012)

Perhaps you should drop a note to the port maintainer and see what he thinks.


----------



## wblock@ (Jun 15, 2012)

That kind of problem is better addressed with the upstream.  The port maintainer could patch it, but the author is the one who should fix it and then it will be fixed for all platforms.


----------



## gordon@ (Jun 15, 2012)

You are getting whatever value happens to be on the stack or in the return register. Totally random garbage (or not so random depending on how your system is set up).


----------

