title: How to handle exception when scanf of integer gets a character
body: <p>The following simple program would give an endless loop when the input is a character, though it meant to tell a character from a digit. How to test if <code>scanf</code> gets a character when it is supposed to be a digit using the return value of <code>scanf</code>?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main() {
    int n;
    int return_value = 0;
  
    while (!return_value) {
      printf("Input a digit:");
      return_value = scanf("%d", &amp;n);
    }
  
    printf("Your input is %d\n", n);
  
    return 0;
  }
  </code></pre>