LearnFrenzy
Sign in Take a free test

Decision Making · Predict the Output · Question 12

C23 bool values from 2 and 0.5 – output Answer, explanation & discussion

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

What is the output of the following program, compiled as C23?

main.c
#include <stdio.h>

int main(void) {
    bool flags[] = {true, false, 2, 0.5};
    int total = 0;
    for (int i = 0; i < 4; i++)
        total += flags[i];
    printf("%d %zu\n", total, sizeof flags);
    return 0;
}
  • A 3 4 Correct
  • B 3.5 4
  • C 4 16
  • D 2 4
Correct Answer: Option A

Every non-zero value converts to true, so 2 and even 0.5 become 1: total = 1 + 0 + 1 + 1 = 3. Each bool takes 1 byte. In C23 bool, true and false need no header.

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