title: Working of scanf and checking if input is int
body: <p>I wanted to check whether input given is integer input or not. I did not wanted to store input in a string. After seeing several questions on stackoverflow and by hit and trial, I have created following code</p>
  
  <pre><code>while(scanf("%d%c",&amp;num,&amp;a) != 2 || a != '\n')
  {
      printf("Please enter an integer only : ");
      if(a == '\n')
          scanf("%c",&amp;a);
      else
      {
          while(a != '\n')
              scanf("%c",&amp;a);
      }
  }
  </code></pre>
  
  <p>It works but according to my understanding, the following should have also worked</p>
  
  <pre><code>while(scanf("%d%c",&amp;num,&amp;a) != 2 || a != '\n')
  {
      printf("Please enter an integer only : ");
      while(a != '\n')
          scanf("%c",&amp;a);
  }
  </code></pre>
  
  <p>Can someone please tell me why above did not worked ?? Also if someone has better solution please give it also. </p>
  
  <p>Note  :I am considering 12qwe also as an invalid input. I just want integers.</p>