title: The Loop continues forever
body: <p>The following code compiles and works as expected, despite one frustrating error in program flow that I don't understand ..<br>
  The loop in the middle of the main function works fine if I pass 2 or 5 as input. However, when I pass -3 or anything below zero (such as a character, which return -1), the loop continues forever and the program doesn't even pause for me to provide input for the scanf function ..</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  void getNum(char * prompt, int*num)
  {
      printf("%s", prompt);
      scanf("%d", num);
  }
  
  int main(int argc, char ** argv)
  {
      int num = -1;
      while(num &lt; 0) { // problem here
          getNum("Number of times you go to the gym in a week: ", &amp;num);
      }
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>I wonder were the mistake is ..</p>
  
  <p>I noticed something strange .. When I change the loop to a do-while loop it works just fine ..</p>
  
  <pre><code>int main(int argc, char ** argv)
  {
      int num;
      do {
          getNum("Number of times you go to the gym in a week: ", &amp;num);
      } while (num &lt; 0); // this works fine ..
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>Also, for some reason, I recompiled the code and it worked fine ..</p>
  
  <p>Can anybody explain this ?</p>