title: Correct usage of strtol
body: <p>The program below converts a string to long, but based on my understanding it also returns an error. I am relying on the fact that if <code>strtol</code> successfully converted string to long, then the second parameter to <code>strtol</code> should be equal to NULL. When I run the below application with 55, I get the following message. </p>
  
  <pre><code>./convertToLong 55
  Could not convert 55 to long and leftover string is: 55 as long is 55
  </code></pre>
  
  <p>How can I successfully detect errors from strtol? In my application, zero is a valid value.</p>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  static long parseLong(const char * str);
  
  int main(int argc, char ** argv)
  {
      printf("%s as long is %ld\n", argv[1], parseLong(argv[1]));
      return 0;
   }
  
  static long parseLong(const char * str)
  {
      long _val = 0;
      char * temp;
  
      _val = strtol(str, &amp;temp, 0);
  
      if(temp != '\0')
              printf("Could not convert %s to long and leftover string is: %s", str, temp);
  
      return _val;
  }
  </code></pre>