title: How can I prevent scanf() to wait forever for an input character?
body: <p>I want to fulfill the following things in a console application:</p>
  
  <ol>
  <li>If user inputs a character, the application will do the
  corresponding task. For example, if user inputs <code>1</code>, the program
  will do task 1, if user inputs <code>q</code>, the program will quit; </li>
  <li>If user inputs nothing, the program will do the default task every 10 seconds (the time needn't to be very strict).</li>
  </ol>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  
  char buff[64];
  char command;
  
  while(command != 'q')
  {
     begin:
     printf("&gt;&gt; ");
     scanf("%s", buff);
     command = buff[0];
  
     switch (command)
     {
         case '1':
         // task 1 code will be added here;
            break;
         case '2':
         // task 2 code will be added here;
            break;
         case 'q':
            printf("quit the loop.\n");
            break;
     }
  
     // wait for 10 seconds;
     Sleep(10000);
     // default task code will be added here;
  
    if(command != 'q')
    {
       goto begin;
    }
  
  }
  </code></pre>
  
  <p>But the problem is the program will trap at the line of <code>scanf()</code> function forever to wait for an input character, if no character is entered. So I'm wondering how to skip the line of <code>scanf()</code> after a certain time, I mean for example, if no input after 1 second, the program can continue, so as to fulfill the second thing listed above.</p>
  
  <p>The platform is Windows, if it matters.</p>
  
  <p><sub>I've removed the semicolon after the <code>while()</code> it was an obvious mistake.</sub></p>