首页 > 编程技术 > C语言

解析一个有关sizeof用法的题目--sizeof(i++)

发布时间:2020-4-25 17:45

复制代码 代码如下:

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

这三行输出应该是什么?
答案是:
10
4
10
第三个为什么不是11? i为什么没有自增?
请看C++标准;
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.
也就是说,如果sizeof的操作数是一个表达式的话,这个表达式时不会被计算的。
sizeof当预处理看就行了,它后面括号里的东西,根本不求值,只根据C的一堆规则判断结果类型,然后返回结果类型的大小
另外一个操作符typeid也是如此。
标签:[!--infotagslink--]

您可能感兴趣的文章: