如果该函数未真正提供一个函数,则该函数调用的返回值是多少? [英] What's the return value from a function call if that function doesn't really provide one

查看:130
本文介绍了如果该函数未真正提供一个函数,则该函数调用的返回值是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有以下代码:

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.

这篇关于如果该函数未真正提供一个函数,则该函数调用的返回值是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆