LearnFrenzy
Sign in Take a free test

Decision Making · Predict the Output · Question 11

An if without braces and misleading indentation Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int x = 3, count = 0;
    if (x > 5)
        count++;
        count += 10;
    printf("%d\n", count);
    return 0;
}
  • A 10 Correct
  • B 0
  • C 11
  • D 1
Correct Answer: Option A

Without braces only the first statement belongs to the if. count += 10; always runs, despite its indentation. Compilers warn (-Wmisleading-indentation); always use braces.

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