LearnFrenzy
Sign in Take a free test

C Variables · Predict the Output · Question 5

A for-loop variable with the same name as an outer one Answer, explanation & discussion

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

What is the output of the following C program?

main.c
#include <stdio.h>

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

The loop declares a new i that exists only inside the loop and shadows the outer one. The outer i is never touched and 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