Why does the system function always return a shifted exit status in C++?

In the C and C++ standards, system returns an implementation-defined value.

In POSIX systems, the return value contains a combination of different values, which can be extracted with macros. To get the program's exit status, you use WEXITSTATUS(return_value), which on your system is defined to be a right shift by 8. The lower 8 bits contain other values (letting you determine, for example, if the program exited normally or due to a signal, etc.)


You are supposed to read the return value using

WEXITSTATUS(code)

https://man7.org/linux/man-pages/man3/system.3.html

Tags:

C++