LearnFrenzy
Sign in Take a free test

C Variables · Predict the Output · Question 2

extern inside a block refers to the global – output Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int n = 5;

void bump(void) { n++; }

int main(void) {
    int n = 1;
    bump();
    bump();
    {
        extern int n;
        printf("%d ", n);
    }
    printf("%d\n", n);
    return 0;
}
  • A 7 1 Correct
  • B 1 1
  • C 7 7
  • D 5 1
Correct Answer: Option A

bump() changes the global n, so it is 7. Inside the inner block, extern int n; re-declares the file-scope n, so it prints 7; after the block, the local n (1) is visible again.

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