返回int main的代码-是否有上限?

Return codes for int main - is there a cap?

本文关键字:是否 int main 代码 返回      更新时间:2023-10-16

我今天一直在使用返回代码,为此我在bash中创建了一个for循环来进行测试:

for RC in {1..300}
do
    echo "int main(void) { return $RC; }" > test.cpp
    g++ test.cpp -o test
    ./test
    echo $?
done

然而,在达到255之后,它似乎又从0开始。为什么呢?

,

252
253
254
255
0
1

因为在posix兼容的系统中返回码被限制在[0-255]范围内。

关于Bash,这里有一个关于退出状态的方便参考。