Having the following variables declarations in the loop, what is the result of compilation and execution of the code?

#include <iostream>

int main() {
   int i;
   for (i = 0; i < 4; i++) {
      static int j = ++i;
      int i = 6+ ++j;
      std::cout << i << ",";
The declaration of a variable i inside the for block is completely valid. It (the declaration) is hiding an external variable i. The line, which initializes the static variable j, is executed only once, so on this line i variable will not be incremented and the value of j will not change in subsequent iterations of the loop.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Sign Up Now
or Subscribe for future quizzes