问题
Let's say we have following code:
int func(char str[], int len) {
// Don't return anything here.
}
int main() {
char str[] = "Hello";
int result = func(str, strlen(str));
printf("%d\n", result);
}
It will print some string value -1679929632 on my computer. And it changes from time to time when I execute.
Can anyone explain why this happen?
回答1:
If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined. If a return value is not required, declare the function to have void return type; otherwise, the default return type is int.
As mentioned above its undefined so finding root cause behind some random value as return will be useless.
来源:https://stackoverflow.com/questions/28060029/whats-the-return-value-from-a-function-call-if-that-function-doesnt-really-pro