LearnFrenzy
Sign in Take a free test

C Variables · Find the Error · Question 2

Error: using a variable outside the block that declared it Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

int main(void) {
    int marks = 72;
    if (marks >= 40) {
        const char *result = "pass";
    }
    printf("%s\n", result);
    return 0;
}
  • A marks must be const
  • B result is declared inside the if block, so it does not exist after the closing brace Correct
  • C printf needs %c
  • D There is no bug
Correct Answer: Option B

Block scope ends at the closing brace. Declare result before the if (with a default such as "fail") and assign it inside.

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