This question already has an answer here:
这个问题在这里已有答案:
- What should I do to get the whole return value of c-program from command line? 2 answers
我该怎么做才能从命令行获取c-program的整个返回值? 2个答案
I wrote the following program to determine the size of a static array. When I ran it, I got a result I can't explain. I've done some searching on stackexchange and google, but nothing I've read has given me a hint.
我编写了以下程序来确定静态数组的大小。当我运行它时,我得到了一个我无法解释的结果。我已经在stackexchange和google上做了一些搜索,但我读过的内容并没有给我一些提示。
#include <stdio.h>
int main()
{
int arrSize, intSize, elemSize;
int input[9][9];
arrSize = sizeof(input);
intSize = sizeof(int);
elemSize = sizeof(input[0]);
printf("Array: %d, Element: %d, Int: %d\n", arrSize, elemSize, intSize);
return sizeof(input);
}
When I compile and run this program, I get the following result (using linux):
当我编译并运行该程序时,我得到以下结果(使用linux):
./a.out ; echo $?
Array: 324, Element: 36, Int: 4
68
I see from http://c-faq.com/malloc/sizeof.html that sizeof
is computed at compile time, and if I change the return to return sizeof(input[0])
I get 36
which is expected, and I get 4
if I change it to return sizeof(input[0][0])
as expected. So why does sizeof(input)
give 68
in the return, but when stored it gives the expected 324
?
我在http://c-faq.com/malloc/sizeof.html上看到sizeof是在编译时计算的,如果我将返回值更改为返回sizeof(input [0]),我得到36这是预期的,我如果我按预期更改它以返回sizeof(输入[0] [0]),则得到4。那么为什么sizeof(输入)在返回时给出68,但是当存储时它会给出预期的324?
3 个解决方案
#1
7
The exit code for your system must be max 255
we can see that 324 % 256 = 68
.
系统的退出代码必须是最大255,我们可以看到324%256 = 68。
#2
4
After a child process terminated, its parent process could get the status information of this child process by
子进程终止后,其父进程可以获取此子进程的状态信息
waitpid(-1, &status, 0);
And the return status could be extracted from status
by WEXITSTATUS(status)
, according to waitpid(2)
this macro
并且可以通过WEXITSTATUS(状态)从状态中提取返回状态,根据waitpid(2)这个宏
returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to
exit(3)
or_exit(2)
or as the argument for a return statement inmain()
.返回子项的退出状态。这包括子项在exit(3)或_exit(2)的调用中指定的status参数的最低有效8位,或者作为main()中return语句的参数。
Therefore, if your main()
returns 324, the return code you get from shell would be 324 % 256
, i.e. 68.
因此,如果你的main()返回324,你从shell获得的返回码将是324%256,即68。
#3
0
From the bash shell and scripting language reference documentation's "Exit status" section:[2] The exit status of an executed command is the value returned by the waitpid system call or equivalent function. Exit statuses fall between 0 and 255, though, as explained below, the shell may use values above 125 specially. Exit statuses from shell builtins and compound commands are also limited to this range. Under certain circumstances, the shell will use special values to indicate specific failure modes. For more information go to:Exit status in Unix
从bash shell和脚本语言参考文档的“退出状态”部分:[2]执行命令的退出状态是waitpid系统调用或等效函数返回的值。退出状态介于0到255之间,但如下所述,shell可能会特别使用125以上的值。 shell builtins和复合命令的退出状态也仅限于此范围。在某些情况下,shell将使用特殊值来指示特定的故障模式。有关更多信息,请访问:Unix中的退出状态
That's why your program is giving this output because 324 is the size of array but shell only can show up to 256 so
这就是为什么你的程序提供这个输出,因为324是数组的大小,但shell只能显示256
324%256=68;
If you want to output more, you could write it to stdout or to a file instead.
如果要输出更多,可以将其写入stdout或文件。
#1
7
The exit code for your system must be max 255
we can see that 324 % 256 = 68
.
系统的退出代码必须是最大255,我们可以看到324%256 = 68。
#2
4
After a child process terminated, its parent process could get the status information of this child process by
子进程终止后,其父进程可以获取此子进程的状态信息
waitpid(-1, &status, 0);
And the return status could be extracted from status
by WEXITSTATUS(status)
, according to waitpid(2)
this macro
并且可以通过WEXITSTATUS(状态)从状态中提取返回状态,根据waitpid(2)这个宏
returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to
exit(3)
or_exit(2)
or as the argument for a return statement inmain()
.返回子项的退出状态。这包括子项在exit(3)或_exit(2)的调用中指定的status参数的最低有效8位,或者作为main()中return语句的参数。
Therefore, if your main()
returns 324, the return code you get from shell would be 324 % 256
, i.e. 68.
因此,如果你的main()返回324,你从shell获得的返回码将是324%256,即68。
#3
0
From the bash shell and scripting language reference documentation's "Exit status" section:[2] The exit status of an executed command is the value returned by the waitpid system call or equivalent function. Exit statuses fall between 0 and 255, though, as explained below, the shell may use values above 125 specially. Exit statuses from shell builtins and compound commands are also limited to this range. Under certain circumstances, the shell will use special values to indicate specific failure modes. For more information go to:Exit status in Unix
从bash shell和脚本语言参考文档的“退出状态”部分:[2]执行命令的退出状态是waitpid系统调用或等效函数返回的值。退出状态介于0到255之间,但如下所述,shell可能会特别使用125以上的值。 shell builtins和复合命令的退出状态也仅限于此范围。在某些情况下,shell将使用特殊值来指示特定的故障模式。有关更多信息,请访问:Unix中的退出状态
That's why your program is giving this output because 324 is the size of array but shell only can show up to 256 so
这就是为什么你的程序提供这个输出,因为324是数组的大小,但shell只能显示256
324%256=68;
If you want to output more, you could write it to stdout or to a file instead.
如果要输出更多,可以将其写入stdout或文件。