假设我们有以下代码:
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); }它将在我的计算机上打印一些字符串值-1679929632.当我执行时,它会不时发生变化.
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?
推荐答案如果函数定义中没有return语句,则在执行被调用函数的最后一条语句后,控件会自动返回到调用函数.在这种情况下,被调用函数的返回值为未定义.如果不需要返回值,则将函数声明为无效返回类型;否则,返回0.否则,默认返回类型为int.
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.
更多推荐
如果该函数未真正提供一个函数,则该函数调用的返回值是多少?
发布评论