138
Deeshti
5y

i -= --i

Comments
  • 7
    i -= --i evaluates to i = 1 strangely enough. i'd imagine it evaluate to 0.
  • 0
    @Mvzes The wonders of cache bleeding
  • 4
    @Mvzes strange? It is equivalent i - ( i - 1)
  • 2
    @asgs The decrement is evaluated first. So i is decremented and then the value is subtracted from i (which again, should already be decremented). Thus it should be 0
  • 2
    @Mvzes it evaluates to zero in C using the clang compiler 🤔
  • 4
    @Mvzes That's why this code has undefined behavior in C. A variable cannot get updated more than once within a sequence point. Different compilers may generate different code for this.
  • 3
    @12bitfloat I was merely explaining why it was 1 in this case. It is clearly a UB.

    Such kind of code can and must only appear in pointless puzzles
  • 0
    @Deeshti and that's why you do not use increment and decrement operators...
  • 0
    it seems to increment the number, anyway I don't fucking care !!!!. Why anyone would do that... I yet to get job but anyone use this in real world? Why they ask this kind of questions in interview?
  • 6
    @Tarak311 Maybe the reason you don't care has something to do with you not having a job
  • 3
    @Tarak311 two possible reasons why they ask this in an interview :
    A) they want you to say this code has UB.
    B) they are idiots. If they don't know about UB you should look for a different employer 😉.
  • 0
    @kraator UB?

    ... Oh. Undefined behaviour.
Add Comment