jeudi 11 février 2021

Counter intuitive behavior of i-- and --i with equality check? [duplicate]

Here is the C code: Try it online!

#include <stdio.h>
void main() {
 int i = 10;
 if(i == i--) {
  printf("hello");
 }
 if(i == --i)
  printf("hello");
}

My idea here is that i == i-- a will operate from left to right, so it will first check equality and then decrease, but it doesn't seem to happen. On thw flip side, i == --i means i will decrement first and then decrease, means false, but it's becomimg true?!

Why it is so?

Aucun commentaire:

Enregistrer un commentaire