Knowee
Questions
Features
Study Tools

Understanding while loopWrite the correct output in the blanks for the below C code?#include <stdio.h>void main() { int i = 3; while (i--) { int i = 100; i--; printf("i = %d ", i); }}

Question

Understanding while loopWrite the correct output in the blanks for the below C code?#include <stdio.h>void main() { int i = 3; while (i--) { int i = 100; i--; printf("i = %d ", i); }}

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the given C code will be "i = 99 i = 99 i = 99".

Here's the step by step explanation:

  1. The variable i is initialized with the value 3.

  2. The while loop will run as long as i is not zero.

  3. Inside the while loop, a new local variable i is declared and initialized with the value 100.

  4. This local i is then decremented by 1, making it 99.

  5. The printf statement prints the value of the local i, which is 99.

  6. The local i goes out of scope at the end of the loop, and the loop condition checks the original i (which is 3 at the start).

  7. The original i is decremented by the i-- in the while loop condition, and the loop runs again as long as i is not zero.

  8. Steps 3-7 repeat two more times, printing "i = 99" each time, until the original i becomes 0 and the loop ends.

So, the output is "i = 99 i = 99 i = 99".

This problem has been solved

Similar Questions

Understanding while loopWrite the correct output in the blanks for the below C code?#include <stdio.h>void main() { int i = 3; while (i--) { int i = 100; i--; printf("i = %d ", i); }}

With suitable example explain for and while loop in C

What will be the output of the following C code?

CWhat is the output of the following piece of code?int i;i = 10;while (i < 20){ printf("%d", i % 2); i++;}

What would be the output of the following program? #include <stdio.h> int main( ) { int j=1; while (j <= 255) { printf ( "%c %d\n ", j, j ); j++; } return 0; }

1/4

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.