Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program?

#include <stdio.h>
int main()
{
    int x = 3;
    float y = 3.0;
    if(x == y)
        printf("x and y are equal");
    else
        printf("x and y are not equal");
    return 0;
}

 

  • A.x and y are equal
  • B.x and y are not equal
  • C.Unpredictable
  • D.No output

Answer: A

Correct answer: x and y are equal

When an int and a float are compared, the int is converted to floating point first. Both values are then exactly 3.0 - and 3 is one of the values a float can represent exactly - so the comparison succeeds. It is only fractional values such as 0.1 that make such comparisons unreliable.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.