LearnFrenzy
Sign in Take a free test

C Const · Predict the Output · Question 4

A const local inside a loop – output Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 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 < 4; i++) {
        const int sq = i * i;
        printf("%d ", sq);
    }
    printf("\n");
    return 0;
}
  • A 0 1 4 9 Correct
  • B 0 0 0 0
  • C Compilation error: sq is assigned 4 times
  • D 0 1 2 3
Correct Answer: Option A

Each pass creates a new sq and initialises it once – that is not an assignment to a const. const locals are perfectly normal inside loops.

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