为什么像WEXITSTATUSstatus这样的宏以这种方式实现?

问题描述

我是C语言的新手,很抱歉,如果我的问题很简单,以下是使我感到困惑的代码

wait.h

# define WEXITSTATUS(status)    __WEXITSTATUS (status)

waitstatus.h

/* If WIFEXITED(STATUS),the low-order 8 bits of the status.  */
#define __WEXITSTATUS(status)   (((status) & 0xff00) >> 8)

我的问题是:

Q1,注释说(status) & 0xff00) >> 8检索状态的低8位。但是看起来它检索状态的中间8位(或第二个低阶8位)?

第2季度-为什么存在将__WEXITSTATUS (status)WEXITSTATUS(status)连接的中间宏(((status) & 0xff00) >> 8),为什么我们不能将所有内容都放在wait.h中呢?

# define WEXITSTATUS(status)   (((status) & 0xff00) >> 8)

解决方法

  1. waitpid man page中提供了更多说明,如下所述。也就是说,注释不是指传递到宏中的status,而是指所描述位置的status

返回孩子的退出状态。它由状态参数的最低有效8位组成,该状态参数是子对象在对exit(3)或_exit(2)的调用中指定的,或作为main()中return语句的参数而指定的

  1. 很可能是因为它允许__WEXITSTATUS在多个地方使用。