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

前端 未结 7 2205
长情又很酷
长情又很酷 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:09

    Because in the first example, c starts out 10. c++ increments c and returns 10, so the second c now evaluates to 11 since it was incremented. So the ultimate expression evaluated is 10 - 11, which equals -1.

    In the second example, ++c increments c again but returns 12 since it is a pre-increment. The second c evaluates to 12 as well, since it's the new value stored in c. So that expression ultimately is evaluated as 12 - 12, which equals 0.

提交回复
热议问题