title: How to scanf only integer?
body: <p>I want the code to run until the user enters an integer value.</p>
  
  <p>The code works for char and char arrays.</p>
  
  <p>I have done the following:</p>
  
  <hr>
  
  <pre><code>#include&lt;stdio.h&gt;
  int main()
  {
      int n;
      printf("Please enter an integer: ");
      while(scanf("%d",&amp;n) != 1)
      {
          printf("Please enter an integer: ");
          while(getchar() != '\n');
      }
      printf("You entered: %d\n",n);
      return 0;
  }
  </code></pre>
  
  <p>The problem is if the user inputs a float value <code>scanf</code> will accept it.</p>
  
  <pre><code>Please enter an integer: abcd
  Please enter an integer: a
  Please enter an integer: 5.9
  You entered: 5
  </code></pre>
  
  <p>How can that be avoided?</p>