What Does Wexitstatus(Status) Return?

I am trying to understand how WEXITSTATUS(status) works. I have come across a piece of code where the return value of WEXITSTATUS(status) is being added to a variable.

Here is the snippet:

waitpid(-1, &status, 0);
counter += WEXITSTATUS(status);

How can the return value of WEXITSTATUS be calculated?

3

1 Answer

WEXITSTATUS(stat_val) is a macro (so in fact it does not "return" something, but "evaluates" to something).

For how it works you might like to look it up in the headers (which should be #included via <sys/wait.h>) that come with the C-compiler you use.

The implementation of this macro might differ from one C-implementation to the other.

Please note, that this macro only gives a sane value, if the macro WIFEXITED(stat_val) gave you a value unequal to 0.

Verbatim from waitpid()'s POSIX specification:

WEXITSTATUS(stat_val)

If the value of WIFEXITED(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main().


The motivation behind adding up the return code(s?) of a particular program is only known to the code's author and the hopefully existing documentation.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest