Why does sizeof(x++) not increment x?

前端 未结 9 1846
清酒与你
清酒与你 2020-11-22 09:36

Here is the code compiled in dev c++ windows:

#include 

int main() {
    int x = 5;
    printf(\"%d and \", sizeof(x++)); // note 1
    print         


        
9条回答
  •  生来不讨喜
    2020-11-22 10:16

    sizeof runs at compile-time, but x++ can only be evaluated at run-time. To solve this, the C++ standard dictates that the operand of sizeof is not evaluated. The C Standard says:

    If the type of the operand [of sizeof] is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

提交回复
热议问题