title: Get User Input without Blocking Endless loop
body: <p>I have written a simple C program that basically consists of an endless loop that counts upwards. During the loop, the user is asked for input- and here comes the tricky part: the loop should NOT be blocked while waiting on the user, but display his input as soon as he entered it:</p>
  
  <pre><code>int main(void){
    int i;
    char dec;
  
    for(;;i++){
      printf("%d\n", i);
      sleep(5);
      if(i==4 || i==8){
          printf("Please enter Y or N\n");
          dec = fgetc(stdin);
          printf("%c\n", dec);
      }
    }
  
  return 0;
  }
  </code></pre>
  
  <p>I found a similiar question for Python here <a href="https://stackoverflow.com/questions/9013078/how-to-read-users-input-when-in-loop-and-without-blocking-work-in-this-loop">Python</a>. So do I need to push the user interaction into a new thread with pthread or is there an easier option?</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>int main(void){
  int i=0;
  char dec;
  
  fd_set input_set;
  
  for(;;i++){
      printf("%d\n", i);
      sleep(2);
      if(i==4 || i==8){
  
          FD_ZERO(&amp;input_set );   /* Empty the FD Set */
          FD_SET(0, &amp;input_set);  /* Listen to the input descriptor */
          dec = select(1, &amp;input_set, NULL, NULL, 0);
  
      }
  }
  
  return 0;
  }
  </code></pre>