LearnFrenzy
Sign in Take a free test

Control Instructions · Predict the Output · Question 7

Type of the conditional operator: sizeof(flag ? 1 : 2.0) Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

What is the output of the following C program?

main.c
#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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links