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

Discussion

What will be the output of the program?

#include <stdio.h>
int main()
{
    float a = 0.7;
    if(0.7 > a)
        printf("Hi\n");
    else
        printf("Hello\n");
    return 0;
}

 

  • A.Hi
  • B.Hello
  • C.Hi Hello
  • D.None of above

Answer: A

Correct answer: Hi

0.7 written in the source is a double, while a is a float, which holds fewer bits. Storing 0.7 in a float loses precision, and when a is promoted back to double for the comparison it is slightly less than the double 0.7. So the condition is true and Hi is printed - the classic warning against comparing floating-point values of different types for equality or order.

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

Post your comment

Please log in or register to post a comment.