title: How I can handle integer overflow?
body: <p>I am trying to handle integer overflow. My code is :</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;string.h&gt;
  #include&lt;errno.h&gt;
  #include&lt;limits.h&gt;
  
  int isInt (char *s)
  {
   char *ep = NULL;
   long i = strtol (s, &amp;ep, 10);
   if ((*ep == 0) || (!strcmp(ep,"\n")))
      return 1;  // it's an int
  
   return 0;  
  } 
  
  int main()
  {
  char *buffer = NULL;
  size_t count = 0;
  ssize_t ret;
  //AMINO *a_acid;
  int num;
  
  for(;;)
  {   
  printf("Please enter an integer:");
  if((ret = getline(&amp;buffer, &amp;count, stdin)) &lt; 0)
  {
      perror("getline: error\n");
      free(buffer);
      exit(EXIT_FAILURE);
  }
  
  if(!isInt(buffer))
  {
      perror("you are not entering int , Try again:");
      continue;
  }
  sscanf(buffer, "%d",&amp;num);
  printf("%d\n", num);
  if ((num &gt; INT_MAX)|| (num &lt; 0))
  {
      perror("you overflowed int variable , Try again:\n ");
      continue;
  }
  break;
  }
  
  }
  </code></pre>
  
  <p>Now I was checking how this code is responding. And I saw something weird.When I am entering so big number, then it is detected. But sometimes is not getting detected.
  Here is my terminal view:</p>
  
  <pre><code>&gt; nazmul@nazmul-Lenovo-G50-80:~/2nd_sem/biophysics$ gcc torson.c 
  &gt; nazmul@nazmul-Lenovo-G50-80:~/2nd_sem/biophysics$ ./a.out
  &gt; Please enter an integer:ksdjfjklh 
  &gt; you are not entering int , Try again:: Success
  &gt; Please enter an integer:338479759475637465765
  &gt; -1 
  &gt; you overflowed int variable , Try again:  : Numerical result out of  
  &gt; range 
  &gt; Please enter an integer:58678946895785 
  &gt; 1103697833
  &gt; nazmul@nazmul-Lenovo-G50-80:~/2nd_sem/biophysics$
  </code></pre>
  
  <p>*Why it is working for this number 338479759475637465765. But it is not working for 58678946895785. logic , I used in my program, is when it is out of bound, then int variable gives some -1 or negative value. I read many article, still it is not quite clear. </p>