Strange status code changes

I have a simple program for checking exit codes:

int main() { 
    return 2000;
}

In the terminal, if I run the program, enter:

echo $?

The terminal responds with 208. What kind? Why do this?

+4
source share
3 answers

While the return value mainis int(usually 32 bits), only 8 bits of the exit code are transmitted to the rest of the operating system.

As Mystic have , 2000 % 256 = 208.

Linux wait waitpid ( exit s). , int, , , , / .. WEXITSTATUS.

int status;
waitpid(childpid, &status, 0);
if (WIFEXITED(status))
    printf("Child exited with code %d\n", WEXITSTATUS(status));

, man!

, :

kernel/exit.c , exit:

888  SYSCALL_DEFINE1(exit, int, error_code)
889  {
890      do_exit((error_code&0xff)<<8);
891  }

, int 15: 8 , do_exit.

:

+6

UNIX , 256, 208.

+2

C : 0, EXIT_SUCCESS EXIT_FAILURE. ( EXIT_SUCCESS 0, , .)

0 EXIT_SUCCESS, [ ] ". EXIT_FAILURE, " ".

. , 2000 208 . .

+2
source

All Articles