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

后端 未结 4 950
清酒与你
清酒与你 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:26

    Here 0xff00 is a binary mask (link text). ANDing it with a value sets all bits to zero, except the second byte (counting from the right).

    You should only use WEXITSTATUS on a process which is known to have exited normally. This information is given by the WIFEXITED macro.

    And the real question in this topic, is the same thing to call this macro in my code as dividing by 256?

    The macro makes the code more readable, and it's guaranted to work on any Posix-compliant implementation. As far as I know Posix doesn't specify the format of the status, so you can't expect your code to work everywhere.

提交回复
热议问题