LearnFrenzy
Sign in Take a free test

Data Types · Predict the Output · Question 10

Converting 5 and 0.1 to bool – output Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

What is the output of the following C program on 64-bit Linux or macOS?

main.c
#include <stdbool.h>
#include <stdio.h>

int main(void) {
    bool b = 5;
    bool c = 0.1;
    printf("%d %d %d\n", b, c, b + c);
    return 0;
}
  • A 5 0 5
  • B 1 0 1
  • C 1 1 2 Correct
  • D 5 1 6
Correct Answer: Option C

Converting any non-zero value to bool gives 1 (true) – even 0.1, which an int conversion would turn into 0. In b + c both are promoted to int: 2.

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