strtol()

Syntax
  #include <stdlib.h>
  
  long strtol(const char *s, char **end, int base);

  
Description

strtol() converts string s into a longint of base base, skipping over any white space at the beginning of s. It stops scanning when it reaches a character not matching the required syntax (or a character too large for a given base) and returns a pointer to that character in *end.

strtol() accepts the following number format:

  Int_Number = 
  

Dec_Number|Oct_Number|Hex_Number|Other_Number

  Dec_Number = SignDigit{Digit} 
  
  Oct_Number = Sign0{OctDigit} 
  
  Hex_Number = 0(x|X)Hex_Digit{Hex_Digit} 
  
  Other_Number = SignOther_Digit{Other_Digit} 
  
  Oct_Digit = 0|1|2|3|4|5|6|7 
  
  Digit = Oct_Digit|8|9 
  
  Hex_Digit = Digit |A|B|C|D|E|F|a|b|c|d|e|f 
  
  Other_Digit = Hex_Digit|<any char between G and Z>|<any char 
  between g and z>

  

The base must be 0 or in the range from 2 to 36. If it is between 2 and 36, strtol() converts a number in that base (digits larger than 9 are represented by upper or lower case characters from A to Z). If base is zero, the function uses the prefix to find the base. If the prefix is 0, strtol() assumes base 8 (octal). If it is 0x or 0X, strtol() assumes base 16 (hexadecimal). Any other prefixes make strtol() scan a decimal number.

Return

The number read. If no number is found, returns zero; if the value is smaller than LONG_MIN or larger than LONG_MAX, returns LONG_MIN or LONG_MAX and sets errno to ERANGE.

See also

atoi()

atol()

scanf()

strtod()

strtoul()