LearnFrenzy
Sign in Take a free test

C Variables · Find the Error · Question 4

Link error: extern variable that is never defined Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

This program compiles without errors but cannot be built into an executable. Why?

main.c
#include <stdio.h>

extern int max_users;

int main(void) {
    printf("%d\n", max_users);
    return 0;
}
  • A extern cannot be used with int
  • B extern only declares max_users; no file defines it, so the linker reports an undefined symbol Correct
  • C max_users must be initialised in main()
  • D There is no bug; it prints 0
Correct Answer: Option B

The compiler trusts the declaration; the linker then finds no definition anywhere ("Undefined symbols … _max_users" / "undefined reference to max_users"). Add exactly one definition, e.g. int max_users = 100; in one .c file.

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