strtol()

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

Description

strtol() converts string s into a long int 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. The number format strtol() accepts is:

Listing: Number Format


Int_Number      = Dec_Number|Oct_Number|                   Hex_Number|Other_Num 
Dec_Number      = SignDigit{Digit} 
Oct_Number      = Sign0{OctDigit} 
Hex_Number      = 0(x|X)Hex_Digit{Hex_Digit} 
Other_Num       = 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, base 8 (octal) is assumed. If it is 0x or 0X, base 16 (hexadecimal) is taken. Any other prefixes make strtol() scan a decimal number.

Return

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

See also

atoi(),

atol(),

scanf(),

strtod(), and

strtoul()