Open In App
Related Articles

C Quiz – 104 | Question 3

Improve Article
Improve
Save Article
Save
Like Article
Like

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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 08 Nov, 2021
Like Article
Save Article
Similar Reads