LearnFrenzy
Sign in Take a free test

Declarations and Initializations · Find the Error · Question 7

Error: extern variable with an initializer inside a function Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

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

main.c
#include <stdio.h>

int main(void) {
    extern int limit = 5;
    printf("%d\n", limit);
    return 0;
}
  • A extern cannot be used with int
  • B An extern declaration at block scope only refers to an object defined elsewhere; it cannot also define it with an initializer Correct
  • C limit must be static
  • D There is no bug
Correct Answer: Option B

Inside a function, extern int limit; refers to a file-scope definition. Defining it needs a file-scope declaration such as int limit = 5; outside main().

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