为什么没有一个return语句返回值12主要功能? [英] Why does a main function without a return statement return value 12?
问题描述
我写了打印表格的程序。我没有包括在主函数的返回语法,但仍然每当我输入echo $?它显示12。
I have written a program that prints a table. I have not included the return syntax in the main function, but still whenever I type echo $? it displays 12.
我的源$ C $ C:
My source code :
#include <stdio.h>
int main(void)
{
int ans,i,n;
printf("enter the no. : ");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
ans = n*i;
printf("%d * %d = %d\n",n,i,ans);
}
}
我没有写回12,但它毕竟我每次执行程序时返回12。
I have not written return 12, but still it returns 12 every time I execute the program.
感谢。
推荐答案
<击>作为swegi说,这是不确定的行为。击>由于史蒂夫·杰索普等人说,这是C99之前未指定的行为,并在C99规定(观察到的行为是不符合标准的,以C99)
As swegi says, it's undefined behavior. As Steve Jessop et al say, it's unspecified behavior before C99, and specified in C99 (the observed behavior is non-conformant to C99)
什么实际发生在大多数环境是,从返回值的最后一个的printf
留在用于返回值的寄存器。
What actually happens in most environments is that the return value from the last printf
is left in the register used for return values.
所以,这将是11对n == 0,12,如果n是一个数字,14两位数N,16三个数字n等。
So it'll be 11 for n == 0, 12 if n is one digit, 14 for two digit n, 16 for three digit n, etc.
这篇关于为什么没有一个return语句返回值12主要功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!