main函数的返回值范围

发布于 2024-10-19 19:08:07 字数 119 浏览 1 评论 0原文

标准对主要返回值范围有何规定? 说最多只能到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

茶底世界 2024-10-26 19:08:07

标准没说。 0EXIT_SUCCESSEXIT_FAILURE 具有(某种)指定的含义。其他任何事情都取决于实施。

目前,大多数基于 Unix 的系统仅支持 8 位返回值。 Windows 支持(至少)32 位返回值。我没有检查64位Windows是否支持64位返回值,但我对此表示怀疑,因为即使64位Windows通常仍然使用32位int。

The standard doesn't say. 0, EXIT_SUCCESS and EXIT_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.

浅黛梨妆こ 2024-10-26 19:08:07

正如其他人所说,C &除了声明

  1. main() 返回一个 int (具有实现定义的大小)和
  2. 零(或 < code>EXIT_SUCCESS) 为成功返回,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

  1. main() returns an int (which is of an implementation defined size), and
  2. zero (or EXIT_SUCCESS) is a successful return and EXIT_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(), or waitid()). wait() and waitpid() 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 added waitid() 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 from main(), calls exit() or abort() or something). The wait() and waitpid() functions return the status by way of a pointer to an integer. The caller extracts the actual exit status using the WIFEXITED(status_val) and WEXITSTATUS(status_val) macros. The latter is defined by POSIX and required to return the low-order 8 bits of the status argument. The waitid() function uses a pointer to a siginfo_t structure to return the process's status information. The si_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 of exec() 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.

空心空情空意 2024-10-26 19:08:07

出口代码在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.

栩栩如生 2024-10-26 19:08:07

C 标准对退出代码没有特别的限制,关于 main 返回值的段落委托给关于 exit() 函数的文档,该文档依次说明:

如果 status 的值为零或EXIT_SUCCESS,则返回状态成功终止的实现定义形式。如果 status 的值为 EXIT_FAILURE,则返回状态失败终止的实现定义形式。否则返回的状态是实现定义的。

除了 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 a DWORD *.

The C standard do not impose particular limitation on exit codes, the paragraph about the return value of main delegates to the documentation about the exit() function, which in turn says:

If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.

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 the wait 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 a DWORD1, but I don't think they'll ever make int be bigger than a DWORD, it would break a lot of code).


1. Because the GetExitCodeProcess parameter reserved for returning this value is a DWORD *.

辞取 2024-10-26 19:08:07

在 Unix 上,等待系统调用设置一个
int 类型的状态值打包为
具有各种类型子项的位字段
终止信息。如果孩子
通过退出终止(如确定
通过 WIFEXITED 宏;平常的
另一种选择是它死于
未捕获的信号),SUS 指定
状态值的低8位
包含退出状态;这个可以
使用 WEXITSTATUS 检索
wait.h 中的宏。因此,在 Unix 上
退出状态仅限于值
0-255,无符号8位的范围
整数。

类 Unix 系统通常使用
成功与失败的零约定
零表示错误。一些约定
已经发展到相对
各种错误代码的含义;为了
例如 GNU 建议使用以下代码
高位设置被保留
严重错误,FreeBSD 有
记录了一系列广泛的
首选解释。

C99 标准仅定义了 0 和 1。但是,允许使用其他值。

有关更多信息,请参阅退出状态 wiki。

On Unix, the wait system call sets a
status value of type int packed as a
bitfield with various types of child
termination information. If the child
terminated by exiting (as determined
by the WIFEXITED macro; the usual
alternative being that it died from an
uncaught signal), SUS specifies that
the lower 8 bits of the status value
contain the exit status; this can
be retrieved using the WEXITSTATUS
macro in wait.h. As such, on Unix
exit statuses are restricted to values
0-255, the range of an unsigned 8-bit
integer.

Unix like systems typically use a
convention of zero for success and non
zero for error. Some conventions
have developed as to the relative
meanings of various error codes; for
example GNU recommend that codes with
the high bit set be reserved for
serious errors, and FreeBSD have
documented an extensive set of
preferred interpretations.

C99 standard defines only 0 and 1. However, allows other values to be used.

See Exit Status wiki for more.

没有伤那来痛 2024-10-26 19:08:07

您返回类型 int。您应该能够返回可以存储在 int 中的任何值。 int 的确切大小取决于实现,因此我无法为您提供确切的范围。

You return type int. You should be able to return any value that can be stored in an int. The exact size of an int is implementation-dependent, so I can't give you an exact range.

懒猫 2024-10-26 19:08:07

5.1.2.2.3 程序终止 1 如果主函数的返回类型是 a
类型与 int 兼容,返回
从最初的调用到主函数
函数相当于调用
退出函数并返回值
以 main 函数作为其参数;
10) 到达终止的}
main 函数返回值 0。如果
返回类型不兼容
int,返回的终止状态
主机环境未指定

http://www.open -std.org/jtc1/sc22/wg14/www/docs/n1124.pdf

即,不需要返回任何内容。然而,它强烈地说明了通常的定义是什么。几乎暗示它们是标准的,但有一张出狱卡,这意味着它可以是任何东西。

5.1.2.2.3 Program termination 1 If the return type of the main function is a
type compatible with int, a return
from the initial call to the main
function is equivalent to calling the
exit function with the value returned
by the main function as its argument;
10) reaching the } that terminates the
main function returns a value of 0. If
the return type is not compatible with
int, the termination status returned
to the host environment is unspecified

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文