Any benefit in using WEXITSTATUS macro in C over division by 256 on exit() status?

后端 未结 4 940
清酒与你
清酒与你 2020-12-03 05:48

I was doing an exercise for university where I had to return a value with exit, that value was actually a count of something. This could be above 255 (which exit() can\'t ha

4条回答
  •  北海茫月
    2020-12-03 06:25

    As far as I can tell from checking the Single Unix Spec, your system happens to store the exit status in the second-to-rightmost octet, but I don't believe the standard does. So, you should use the macros for at least a few reasons:

    • They're correct. Bit-shifting a negative number does different things on different platforms. Does it work the way you want on yours? I don't know.
    • They're simple. Its immediately clear what WEXITSTATUS does. Less so with other approaches. If you saw the hand-rolled version of WIFSIGNALED, would you recognize it? How much longer would it take than WIFSIGNALED.
    • They're portable. Since its how the spec says to do it, it'll work on every system (at least pretty much every Unix-like system).

提交回复
热议问题