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;
}
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.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development