jeudi 5 décembre 2019

Conditional expression in ternary operator in C

Code

#include <stdio.h>

int main() {
  int i;
  for (i=1; i<=10; i++) {
        (i % 2) ? printf("%d is odd\n", i) : printf("%d is even\n", i);
  }
}

Result

1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even

In the above C program, why it still works fine even though the conditional expression only states i%2 and not i%2!=0 ?

Aucun commentaire:

Enregistrer un commentaire