LearnFrenzy
Sign in Take a free test

Declarations and Initializations · Find the Error · Question 1

Error: using a variable before it is declared Answer, explanation & discussion

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

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

main.c
#include <stdio.h>

int main(void) {
    total = 10;
    int total;
    printf("%d\n", total);
    return 0;
}
  • A total must be global
  • B total is used before its declaration; a name is only visible after the point where it is declared Correct
  • C printf needs %ld
  • D There is no bug
Correct Answer: Option B

Scope begins at the end of the declarator, so the first line refers to an undeclared name. Declare (and initialise) before use: int total = 10;.

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