Resolve a question about the use of sizeof sizeof of i++

  • 2020-04-02 01:11:12
  • OfStack


  #include <stdio.h>
  int main()
  {
  int i;
  i = 10;
  printf("%dn", i);
  printf("%dn", sizeof(i++));
  printf("%dn", i);
  return 0;
  }

What should the three lines of output be?
The answer is:
10
4
10
Why is the third one not 11? Why is I not self-increasing?
Look at the C++ standard;
5.3.3 sizeof
The sizeof operator yields The number of bytes in The object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id.
That is, if the operand of sizeof is an expression, the expression will not be evaluated.
Sizeof looks good as a preprocessor, and the thing behind it in parentheses doesn't care about the value at all, it just judges the result type according to a bunch of rules in C, and then returns the sizeof the result type
The same is true of the other operator, typeid.

Related articles: