Search code examples
c++linuxprogram-entry-pointexit-code

Return codes for int main - is there a cap?


I have been playing around with return codes today for which I created a for loop in bash to test it out:

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

However after reaching 255 it seems to begin from 0 again. Why is that?

EG:

252
253
254
255
0
1

Solution

  • Because return codes in POSIX-compliant systems are limited to the range [0-255].

    With regard to Bash, here is a handy reference on exit statuses.