实现代码如下:

  #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也是如此。

以上就是【解析一个有关sizeof用法的题目--sizeof(i++)】的全部内容了,欢迎留言评论进行交流!

赞(0) 踩(0)

与本文相关的软件

发表我的评论

最新评论

  1. 暂无评论