不存在return语句时,int类型return语句的返回值 [英] Return value for a int type return statement when there is no return statement present
问题描述
不存在return语句时,int类型return语句的返回值是什么
What is the return value for an int type return statement when there is no return statement present
为什么?
int func()
{
printf("Hello");
}
int func1()
{
}
void main()
{
int s,p;
s=func();
p=func1();
printf("%d %d", s, p);
}
推荐答案
您在评论中说:
在func()的情况下,我得到的返回值为5
in case of func() I am getting a return value of 5
这次,实际上是在您的计算机上发生的事情是 printf
的返回值(即5,即打印的字符数)回到了func
在寄存器中. func
尚未在自己返回之前修改该寄存器,因此 main
在检查以下内容时会看到什么似乎是 func
的返回值:相同的寄存器.
What appears to have happened, in practice, on your machine, this time, is that the return value of printf
(which is 5, the number of characters printed) came back to func
in a register. func
has not modified that register prior to its own return, and so main
sees what appears to be the return value of func
when it checks that same register.
您可以通过查看反汇编的 func
可执行代码来确认或驳斥此问题.
You could confirm or refute this by looking at the disassembled executable code for func
.
如果这确实发生了,那么这是正在使用的调用约定的详细信息,并且偶然情况是 func
在调用 printf
之后不使用寄存器.正如基里连科所说,这种行为在形式上是未定义的,您无权依赖它.
If this really is what has happened then it is a detail of the calling convention in use, and happenstance that func
doesn't use the register after calling printf
. As Kirilenko says the behavior is formally undefined, and you are not entitled to rely on it.
这篇关于不存在return语句时,int类型return语句的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!