LearnFrenzy
Sign in Take a free test

C Variables · Predict the Output · Question 3

A static array without an initializer – output Answer, explanation & discussion

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

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    static int counts[5];
    counts[2] += 3;
    printf("%d %d\n", counts[2], counts[4]);
    return 0;
}
  • A 3 0 Correct
  • B 3 garbage
  • C garbage garbage
  • D 0 0
Correct Answer: Option A

A static array has static storage duration, so every element starts at 0 – even without an initializer. Without static, all five elements would be indeterminate.

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