LearnFrenzy
Sign in Take a free test

C Variables · Predict the Output · Question 6

A local declared in a loop body is re-created each pass Answer, explanation & discussion

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

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    for (int k = 0; k < 3; k++) {
        int total = 0;
        total += k;
        printf("%d ", total);
    }
    printf("\n");
    return 0;
}
  • A 0 1 2 Correct
  • B 0 1 3
  • C 3 3 3
  • D 0 0 0
Correct Answer: Option A

total is created and set to 0 at the start of every pass, so it never accumulates. To keep a running total, declare it before the loop.

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