LearnFrenzy
Sign in Take a free test

Control Instructions · Predict the Output · Question 2

Dangling else with misleading indentation – output Answer, explanation & discussion

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

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a = 5, b = -1;
    if (a > 0)
        if (b > 0)
            printf("A\n");
    else
        printf("B\n");
    printf("done\n");
    return 0;
}
  • A B done Correct
  • B done
  • C A done
  • D A B done
Correct Answer: Option A

Despite the indentation, the else belongs to if (b > 0). a > 0 is true and b > 0 is false, so the inner else prints B.

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