LearnFrenzy
Sign in Take a free test

C Variables · Predict the Output · Question 10

A global and a static local with the same name Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int count = 100;

void tick(void) {
    static int count = 0;
    count++;
    printf("%d ", count);
}

int main(void) {
    tick();
    tick();
    printf("%d\n", count);
    return 0;
}
  • A 1 2 100 Correct
  • B 101 102 102
  • C 1 2 2
  • D 101 102 100
Correct Answer: Option A

Inside tick(), the static local count shadows the global, so only the local goes 1, 2. main() sees the global, which is still 100.

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