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

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

    Whether it outputs 0 or -1 for the first println and 0 or 1 for the second is undefined. The compiler is at liberty to evaluate the left or right hand side first and so whether the increments have taken effect by the time the right hand side is evaluated will depend on the compiler (and indeed the compiler could do one thing one time and another the next)

    The increments are only guaranteed to take place at the next sequence point. Subtraction is not a sequence point.

    EDIT: Bah, wrote that answer when the question was tagged C/C++. Sequence points don't apply to Java. I've left the answer in case others find it useful when considering C/C++ expressions

提交回复
热议问题