ProgrammingVery hardGATE CS
What is printed? Assume IEEE-754 float (32-bit) and double (64-bit) with no extended-precision evaluation.
What is printed? Assume IEEE-754 float (32-bit) and double (64-bit) with no extended-precision evaluation.
#include <stdio.h>
int main(void) {
float f = 0.1f;
double d = 0.1;
printf("%d %d %d", f == d, (float)d == f, f == 0.1f);
return 0;
}- A. 0 0 1
- B. 1 0 0
- C. 1 1 1
- D. 0 1 1
Correct answer: D. 0 1 1
f == d promotes f to double: the float nearest 0.1 is 0.10000000149..., which differs from the double nearest 0.1, so the result is 0. Rounding d to float gives the same float as 0.1f, and 0.1f == f is trivially 1.
More C Programming questions
- What is printed? c int a[5] = {1, 2}; printf("%d %d %zu", a[1], a[2], sizeof a / sizeof a[0]);
- What is printed? c printf("%.1f %d", 7 / 2 2.0, (int)(7 / 2.0 2));
- What is printed? c printf("%d %d", 6 & 3 << 1, ~5 + 1);
- What is printed? c int n = 2, s = 0; switch (n) { case 1: s += 1; case 2: s += 2; case 3: s += 3; break; default: s += 9; } printf("%d", s);
- What is printed? c int a[] = {10, 20, 30, 40}; int p = a + 1; printf("%d %d %d", (p + 1), p[-1], (int)(&a[3] - p));
- What is printed? c char s[] = "hello"; s[2] = '0'; printf("%zu %zu %s", sizeof s, strlen(s), s + 3);