zoukankan      html  css  js  c++  java
  • sizeof问题

    #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

    11

    但是却出人意外的输出了

    10

    4

    10

    这个问题实际上就是对于sizeof操作符的使用问题, sizeof不是函数,只是一种操作符,对于其后的表达式,根本不会进行计算的。sizeof当预处理看就行了,它后面括号里的东西,根本不求值,只根据C的一堆规则判断结果类型,然后返回结果类型的大小,,


    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 anunevaluated operand (Clause 5), or a parenthesized type-id.”


    也就是说,如果sizeof的操作数是一个表达式的话,这个表达式时不会被计算的。

    更有甚者

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



    点击打开链接



  • 相关阅读:
    看别人的代码学习的css
    Font Awesome
    响应式网站设计
    css兼容性的问题
    英语
    我的bootstrap使用的历程
    jquery的常用的容易忘记的东西
    jquery基本方法
    js与jquery的区别
    134123
  • 原文地址:https://www.cnblogs.com/xuddong/p/3071739.html
Copyright © 2011-2022 走看看