Open In App

C Quiz – 104 | Question 3

What’s going to happen when we compile and run the following C program?




#include < stdio.h ><br>
<br>
int main()<br>
{<br>
  int i = 1, j;<br>
  for ( ; ; )<br>
  { <br>
    if (i)<br>
        j = --i;<br>
    if (j < 10)<br>
       printf("GeeksQuiz", j++);<br>
    else<br>
       break;<br>
  }<br>
  return 0;<br>
}<br>

(A) Compile Error.
(B) No compile error but it will run into infinite loop printing GeeksQuiz.
(C) No compile error and it’ll print GeeksQuiz 10 times.
(D) No compile error but it’ll print GeeksQuiz 9 times.

Answer: (C)
Explanation: Basically, even though the for loop doesn’t have any of three expressions in parenthesis, the initialization, control and increment has been done in the body of the loop. So j would be initialized to 0 via first if. This if itself would be executed only once due to i–. Next if and else blocks are being used to check the value of j and existing the loop if j becomes 10. Please note that j is getting incremented in printf even though there’s no format specifier in format string. That’s why GeeksQuiz would be printed for j=0 to j=9 i.e. a total of 10 times.
Quiz of this Question

Article Tags :