title: converting from string to int using strtol() : \0' or '\n'?
body: <p>I have a doubt with conversion from string to int.
  I got a string through function fgets then I used strtol function to convert it to int.
  This is the code :</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 main(void)
  {
    char buf[BUFSIZ];
    char *p = NULL;
    long int val;
    int numero;
    int temp;
  
    do
    {
  temp=0;
  printf ("Enter a number: ");
  if (fgets(buf, sizeof(buf), stdin) != NULL)
  {
      val = strtol(buf, &amp;p, 10);
      if(buf==p)
      {
          printf(" no digits \n");
          temp=1;
      }
      if ((errno == ERANGE &amp;&amp; (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 &amp;&amp; val   == 0)) 
      {
          perror("strtol");
          temp=1;
      }
      if (*p != '\0')
      {
          printf("you have insert any char character \n");
          temp=1;
      }
   }
  else
   {
      printf("Error\n");
      temp=1;
   }   
  }
  while(temp == 1);
  
   /* If we got here, strtol() successfully parsed a number */
   numero=(int)val;
   printf("***** The number is : %d ******* \n",numero);
  
  return 0;
  }
  </code></pre>
  
  <p>and this code doen't work, but it work if I replace this control </p>
  
  <pre><code>if (*p != '\0')
      {
          printf("you have insert any char character \n");
          temp=1;
      }
  </code></pre>
  
  <p>with this one :</p>
  
  <pre><code>if (*p != '\n')
      {
          printf("you have insert any char character \n");
          temp=1;
      }
  </code></pre>
  
  <p>Do you know why ? :)</p>
  
  <p>EDIT : 
  This is the code of my final function version :) Thanks to all :) Now it seem that all correctly works :</p>
  
  <pre><code>int readIN(int *numero)  
  {
     long int val;
     char buf[BUFSIZ];
     char *p = NULL;
  
     if (fgets(buf, sizeof(buf), stdin) != NULL)
     {
        val = strtol(buf, &amp;p, 10);
        if(buf==p)
        {
          return 1;
        }
      if ( (val &gt; INT_MAX || val &lt; 0) || (errno != 0 &amp;&amp; val == 0)) 
      {
             return 1;
              }
      if (*p != '\n' &amp;&amp; *p != '\r' &amp;&amp; *p != '\0')
      {
           return 1;
      }
    }
    else
    {
      return 1;
    }  
  
    *numero=(int)val;
    return 0;  
  }
  </code></pre>