LearnFrenzy
Sign in Take a free test

Decision Making · Predict the Output · Question 7

A tiny double as a condition vs after conversion to int Answer, explanation & discussion

  • Output
  • Medium
  • 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) {
    double d = 0.0001;
    if (d)
        puts("double is true");
    int n = (int) d;
    if (!n)
        puts("int is false");
    return 0;
}
  • A double is true int is false Correct
  • B double is true
  • C int is false
  • D Nothing
Correct Answer: Option A

As a condition, 0.0001 is compared with 0.0 – not equal, so true. Converting to int truncates it to 0, which is false. The type at the moment of the test matters.

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