LearnFrenzy
Sign in Take a free test

C Variables · Predict the Output · Question 1

static and automatic variables inside a loop body Answer, explanation & discussion

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

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    for (int i = 0; i < 3; i++) {
        static int s = 0;
        int a = 0;
        s++;
        a++;
        printf("%d%d ", s, a);
    }
    printf("\n");
    return 0;
}
  • A 11 21 31 Correct
  • B 11 11 11
  • C 11 22 33
  • D 01 11 21
Correct Answer: Option A

The static s is initialised once and keeps counting. The automatic a is created and set to 0 on every pass of the loop body, so it is always 1 after a++.

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