main函数的返回值范围
标准对主要返回值范围有何规定? 说最多只能到255?
因为
int main(void){
return 256;
}
echo $? ; # out 0
What does standard say about main return values range?
Say only up to 255?
Because
int main(void){
return 256;
}
echo $? ; # out 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
标准没说。
0
、EXIT_SUCCESS
和EXIT_FAILURE
具有(某种)指定的含义。其他任何事情都取决于实施。目前,大多数基于 Unix 的系统仅支持 8 位返回值。 Windows 支持(至少)32 位返回值。我没有检查64位Windows是否支持64位返回值,但我对此表示怀疑,因为即使64位Windows通常仍然使用32位int。
The standard doesn't say.
0
,EXIT_SUCCESS
andEXIT_FAILURE
have (sort of) specified meanings. Anything else depends on the implementation.At the present time, most Unix-based systems only support 8-bit return values. Windows supports (at least) a 32-bit return value. I haven't checked whether 64-bit Windows supports a 64-bit return value, but I rather doubt it, since even 64-bit Windows normally still uses a 32-bit int.
正如其他人所说,C &除了声明
main()
返回一个int
(具有实现定义的大小)和EXIT_FAILURE
为不成功返回。它确实指定明确不返回值的
main()
被视为返回零。在这种情况下,返回值的解释取决于等待进程完成的进程(通过调用
wait()
,waitpid()
,或<代码>waitid())。wait()
和waitpid()
是较旧的 POSIX 函数,它们指定仅 返回值的最低有效八位应可供等待的父进程使用。 POSIX:2008 标准添加了waitid()
作为通用等待方法,可以访问子进程的完整退出状态。分叉子进程后,父进程调用
wait*()
函数之一进入休眠状态,直到分叉进程完成(例如,从main()
返回,调用exit()
或abort()
或其他)。wait()
和waitpid()
函数通过指向整数的指针返回状态。调用者使用WIFEXITED(status_val)
和WEXITSTATUS(status_val)
宏提取实际退出状态。后者由 POSIX 定义,并要求返回低 8 位状态参数的。waitid()
函数使用指向siginfo_t
结构的指针来返回进程的状态信息。si_status
成员包含完整状态值,如 状态信息。基本上,退出状态的值是仁者见仁智者见智的。 ANSI/ISO 规范是开放式的。 POSIX 套件有多种方法来等待进程完成并获取其退出状态。 POSIX 还将
spawn()
定义为exec()
的轻量级版本,它对退出状态值有自己的一组约束。 Shell 有进一步限制结果值的习惯 - GNU 的 bash 将返回状态限制为 7 位和 POSIX 兼容 shell 将退出状态值限制为 8 位。 FWIW,大多数人都同意将您的返回值限制为 更低比 64 似乎更安全。As others have stated, the C & C++ Standards don't constrain return values at all other than to state that
main()
returns anint
(which is of an implementation defined size), andEXIT_SUCCESS
) is a successful return andEXIT_FAILURE
is a non-successful return.It does specify that a
main()
that does explicitly not return a value is treated as if it had returned zero.In this case, the interpretation of the return value is up to the process that waits on the process to complete (by calling
wait()
,waitpid()
, orwaitid()
).wait()
andwaitpid()
are the older POSIX functions and they specify that only the least significant eight bits of the return value shall be available to a waiting parent process. The POSIX:2008 standard addedwaitid()
as a generalized wait method that has access to the full exit status of a child process.After forking off a subprocess, the parent process calls one of the
wait*()
functions to sleep until the forked process is completed (e.g., returns frommain()
, callsexit()
orabort()
or something). Thewait()
andwaitpid()
functions return the status by way of a pointer to an integer. The caller extracts the actual exit status using theWIFEXITED(status_val)
andWEXITSTATUS(status_val)
macros. The latter is defined by POSIX and required to return the low-order 8 bits of the status argument. Thewaitid()
function uses a pointer to asiginfo_t
structure to return the process's status information. Thesi_status
member contains the full status value as described in Status Information.Basically, the values of the exit status are in the eye of the beholder. The ANSI/ISO specifications are open-ended. The POSIX suite has multiple ways to wait on a process to finish and fetch it's exit status. POSIX also defines
spawn()
as a lighter-weight version ofexec()
which has its own set of constraints on exit status values. Shells have a habit of further restricting result values -- GNU's bash limits the return status to 7 bits and a POSIX-compliant shell limits exit status values to 8 bits. FWIW, most people agree that restricting your return values to be lower than 64 seems to be safe.出口代码在UNIX类似系统上的0到255之间的数字。您可以返回任何内容,但在Linux中进行了改装256。取得peek 在这里在Linux返回代码上。还有a Wikipedia关于该主题的文章 breifly谈论Windows的退出代码。
Exit codes are a number between 0 and 255 inclusive on Unix like system. You can return anything but in Linux it's modded 256. Take a peek here for a good explanation on Linux return codes. There is also a Wikipedia article on the topic which talks breifly about exit codes for Windows.
C 标准对退出代码没有特别的限制,关于
main
返回值的段落委托给关于exit()
函数的文档,该文档依次说明:除了
EXIT_SUCCESS
/EXIT_FAILURE
准则之外,它基本上意味着“做你想做的事”。:)
正如一条评论中所述,在 POSIX 系统上实际上仅考虑退出代码的低 8 位这一事实只是一种 UNIXism,源自
wait
系统调用被设计(退出状态必须封装在<的低8位中) code>wait return value),与 C 标准无关。一个反例是 Windows,其中传递给
exit
/return
的整个值都会被考虑(只要它不大于DWORD
1,但我认为他们永远不会使int
大于DWORD
,这会破坏很多代码)。1. Because the
GetExitCodeProcess
parameter reserved for returning this value is aDWORD *
.The C standard do not impose particular limitation on exit codes, the paragraph about the return value of
main
delegates to the documentation about theexit()
function, which in turn says:which, apart from the
EXIT_SUCCESS
/EXIT_FAILURE
guidelines, basically means "do whatever you want".:)
As said in one comment, the fact that on POSIX systems only the lower 8 bits of the exit code are actually considered is just a UNIXism, deriving from how the
wait
syscall is designed (the exit status has to be packed in the lower 8 bits of thewait
return value), and has nothing to do with the C standard.A counterexample is Windows, where the whole value passed to
exit
/return
is considered (as long as it's not bigger than aDWORD
1, but I don't think they'll ever makeint
be bigger than aDWORD
, it would break a lot of code).1. Because the
GetExitCodeProcess
parameter reserved for returning this value is aDWORD *
.C99 标准仅定义了 0 和 1。但是,允许使用其他值。
有关更多信息,请参阅退出状态 wiki。
C99 standard defines only 0 and 1. However, allows other values to be used.
See Exit Status wiki for more.
您返回类型
int
。您应该能够返回可以存储在int
中的任何值。int
的确切大小取决于实现,因此我无法为您提供确切的范围。You return type
int
. You should be able to return any value that can be stored in anint
. The exact size of anint
is implementation-dependent, so I can't give you an exact range.http://www.open -std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
即,不需要返回任何内容。然而,它强烈地说明了通常的定义是什么。几乎暗示它们是标准的,但有一张出狱卡,这意味着它可以是任何东西。
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
ie, there is no requirement for it to return anything. However it strongly states just up from that what the usual definitions are. Almost implying they are they standard, but has a get out of jail free card which means it can be anything.