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); }}
Solution
The output of the given C code will be "i = 99 i = 99 i = 99".
Here's the step by step explanation:
-
The variable
iis initialized with the value3. -
The
whileloop will run as long asiis not zero. -
Inside the
whileloop, a new local variableiis declared and initialized with the value100. -
This local
iis then decremented by 1, making it99. -
The
printfstatement prints the value of the locali, which is99. -
The local
igoes out of scope at the end of the loop, and the loop condition checks the originali(which is3at the start). -
The original
iis decremented by thei--in thewhileloop condition, and the loop runs again as long asiis not zero. -
Steps 3-7 repeat two more times, printing "i = 99" each time, until the original
ibecomes0and the loop ends.
So, the output is "i = 99 i = 99 i = 99".
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; }
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.