-4

This code prints both "equal" and "1 0".

Why is this happening even though 1 != 0 ?

if(2/2 == 2/2.0) 
    printf("equal \n");

printf("%d  %d", 2/2, 2/2.0);
fredmaggiowski
  • 2,232
  • 3
  • 25
  • 44
havada
  • 19
  • 1
  • 3

1 Answers1

5

The result of 2/2.0 is of type double. But you are using %d to print it, which is undefined behaviour. Use %lf instead to print a double. That's why you get 0 which is a possible result of invoking undefined behaviour.

2501
  • 25,460
  • 4
  • 47
  • 87
P.P
  • 117,907
  • 20
  • 175
  • 238