为什么 sizeof(x)++ 可以编译?

Why does sizeof(x)++ compile?

我已经 运行 进入以下代码片段:

int a = 3;
printf("%d", sizeof(a)++);

显然这将使用 GCC 9.3.0 和 -std=c99 进行编译。 虽然这不编译:

printf("%d", sizeof(3)++);

GCC 打印错误

error: lvalue required as increment operand

在我编译第一个片段之前,我预计会出现这样的错误。

后缀 ++ 运算符的操作数应为 C99 标准的左值

The operand of the postfix increment or decrement operator shall have qualified or unqualified real or pointer type and shall be a modifiable lvalue.

关于 sizeof 运算符的 return 值(符合预期):

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

The value of the result is implementation-defined, and its type (an unsigned integer type) is size_t, defined in (and other headers).

sizeof(a)++ 怎么可能编译?这是未定义的行为还是我遗漏了什么?

sizeof 是一个运算符,而不是一个函数,作为任何运算符,它都有一个优先级,在 C 语言中它低于 ++ 运算符。因此,构造 sizeof(a)++ 等价于 sizeof a++,后者又等价于 sizeof (a++)。这里我们在 a 上有后增量,这是一个左值,所以它是完全合法的。如果 a 而不是左值 3,则编译将失败。