0

Why this program print nothing.anyone who can explain reasion behind this

#include <stdio.h>

int main(){
    int i = 0;
    for(;;){
        if(i==10)
            continue;
    printf("%d ",++i);
    }
    return(0);
}

enter image description here

  • A couple of suggestions for you: 1. Use consistent indentation (your `printf` should be at the same level of indentation as your `if(i==10)`). 2. Always include a termination condition so that the program doesn't run forever. 3. Always use `{}` rather than only doing so when you have more than one statement -- it's easier to edit later, and harder to get wrong and have hard-to-diagnose bugs. – T.J. Crowder Feb 28 '22 at 11:46

1 Answers1

1

The reason is that printf does not immediately print to the screen. Instead, it caches the input in internal buffer, and when either a newline "\n" character or an explicit flush call arrives - it will print it all at once.

Now in your case, I reaches value of 10 and the program is stuck. Flush or newline never arrive. Try this and see the difference:

printf("%d ",++i);
fflush(stdout);
Yuri Nudelman
  • 2,874
  • 2
  • 17
  • 24