“C - C++” joke about postfix/prefix operation ordering

前端 未结 7 2228
长情又很酷
长情又很酷 2020-12-22 13:34

My friend sent me a joke:

Q. What\'s the difference between C and C++?

A. Nothing, because: (C - C++ == 0)

I tried to change

7条回答
  •  被撕碎了的回忆
    2020-12-22 14:14

    I used this C++ code

    The reason is simple. The ++ operator makes a copy of the var, increase the value then return the copied val. Assuming the operators are evaluated left to right it will pass 10-10(then inc C) thus its always 0. However in most cases depending on order is 'undefined behavior' and some compilers like gcc will report a warning (example http://codepad.org/UPBqO38B)

    int main()
    {
    int c=10;
    printf("%d", c-c++);
    }
    

提交回复
热议问题