Question 7
Back to Exercise
What is the output of the following C program?
#include <stdio.h>
int main(void) {
int flag = 1;
printf("%zu\n", sizeof(flag ? 1 : 2.0));
printf("%.1f\n", flag ? 1 : 2.0);
return 0;
}
- A 8 1.0 Correct
- B 4 1
- C 4 1.0
- D 8 2.0
Correct Answer: Option A
The result type is the common type of both branches: int and double give double (8 bytes). So even when the int branch is chosen, the value is 1.0 – which is why %.1f is correct here and %d would not be.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!