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

前端 未结 9 1654
清酒与你
清酒与你 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:06

    sizeof(foo) tries really hard to discover the size of an expression at compile time:

    6.5.3.4:

    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.

    In short: variable length arrays, run at runtime. (Note: Variable Length Arrays are a specific feature -- not arrays allocated with malloc(3).) Otherwise, only the type of the expression is computed, and that at compile time.

提交回复
热议问题