strtol -- Convert Character String to Long Integer

Format

#include <stdlib.h>
long int strtol(const char *nptr, char **endptr, int base);

Language Level: ANSI, XPG4
strtol converts a character string to a long-integer value. The parameter nptr points to a sequence of characters that can be interpreted as a numerical value of the type long int. When it reads a character that it does not recognize as part of a number, strtol stops reading the string at that character, and sets endptr to point to the remainder of nptr. The character at which strtol stops reading the string may be the null character at the end of that string. The ending character can also be the first numeric character greater than or equal to the base.

When you use the strtol function nptr should point to a string with the following form:

If base is in the range of 2 through 36, it becomes the base of the number. If base is 0, the prefix determines the base (8, 16, or 10): the prefix 0 means base 8 (octal); the prefix 0x or 0X means base 16 (hexadecimal); using any other digit without a prefix means decimal.

Return Value
strtol returns the value represented in the string, except when the representation causes an overflow. For an overflow, it returns LONG_MAX or LONG_MIN, according to the sign of the value and errno is set to ERANGE. If base is not a valid number, strtol sets errno to EDOM.

If the string pointed to by nptr does not have the expected form, no conversion is performed and the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.

Example
This example converts the strings to a long value. It prints out the converted value and the substring that stopped the conversion.

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
   char *string, *stopstring;
   long l;
   int bs;
   string = "10110134932";
   printf("string = %s\n", string);
   for (bs = 2; bs <= 8; bs *= 2)
   {
      l = strtol(string, &stopstring, bs);
      printf("   strtol = %ld (base %d)\n", l, bs);
      printf("   Stopped scan at %s\n\n", stopstring);
   }
   return 0;
   /**************************************************
      The output should be:
      string = 10110134932
      strtol = 45 (base 2)
      Stopped scan at 34932
      strtol = 4423 (base 4)
      Stopped scan at 4932
      strtol = 2134108 (base 8)
      Stopped scan at 932
   **************************************************/
}


atof -- Convert Character String to Float
atoi -- Convert Character String to Integer
atol -- Convert Character String to Long Integer
_atold -- Convert Character String to Long Double
_ltoa -- Convert Long Integer to String
strtod -- Convert Character String to Double
strtold -- Convert String to Long Double
strtoll -- Convert Character String to Long Long Integer
strtoul -- Convert String Segment to Unsigned Integer
strtoull -- Convert String Segment to Unsigned Long Long Integer
wcstod -- Convert Wide-Character String to Double
wcstol -- Convert Wide-Character to Long Integer
wcstoul -- Convert Wide-Character String to Unsigned Long
<stdlib.h>