C ++ programs return an int, so why does return -1 return 255?

Please note that I am running a Linux machine, although I do not think that the result is different from a Windows (or other) machine.

This is a simple question - C ++ programs usually return a 32-bit int. If I return -1, and then print the result from the terminal, the result will be 255.

Why is this? I feel connected, this is what I should have known or should have noticed many years ago. I have never used return codes before, or thought about it before.

C ++ Testing Program:

int main()
{
    return -1;
}

Compile:

g++ main.cpp -o a.out

Run:

./a.out

Check the result:

echo $?

Result:

255

I would expect to see 2 ^ 32 - 1.

Why the result is 255not -1or even 4294967295. ( 2^32 - 1)

+4
2

( ) . 8 . ( , ), "" "" , , " ", , C ++ , [ ] - C int - C , , , 432, C ++.

C , 0 EXIT_SUCCESS ( 0, ) EXIT_FAILURE ( ) . "" , , /, .

, ( - ), main, , , main. main /, , . , , :

  • /
  • , / / .

C ++ , , / C / ++, C ++ - " " - , , .., .

+13

int, 0-255.

, (unsigned char) main() .

:

  • , , POSIX (. exit). .
  • , , , , . C main. exit(your_return_value). , , , C ( C posix, ).
+6

Source: https://habr.com/ru/post/1599643/


All Articles