Machine-translated by Google. Questions are authored in English.
Top

Discussion

#include <stdio.h>
int main()
{
    int i = 3;
    while (i--)
    {
        int i = 100;
        i--;
        printf("%d ", i);
    }
    return 0;
}

 

  • A.Infinite Loop
  • B.99 99 99
  • C.99 98 97
  • D.2 2 2

Answer: B

Correct answer: 99 99 99

The inner int i = 100; shadows the outer i for the whole of the loop body, so i-- and the printf refer to the new variable, which is created afresh at 100 on every pass and printed as 99. The outer i is untouched inside the body and is decremented only by the while condition, so the loop still runs three times.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.