LearnFrenzy
Sign in Take a free test

Declarations and Initializations · Predict the Output · Question 4

A function using an extern variable defined later Answer, explanation & discussion

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

What is the output of the following C program on a 64-bit system?

main.c
#include <stdio.h>

extern int n;

int twice(void) { return n * 2; }

int n = 21;

int main(void) {
    printf("%d\n", twice());
    return 0;
}
  • A 42 Correct
  • B 0
  • C Compilation error
  • D Garbage
Correct Answer: Option A

The extern declaration lets twice() use n before its definition appears. The definition initialises n to 21 before the program starts, so the result is 42.

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