Utility Functions and C Language Reference > C Language Reference > Alphabetical List of Functions > strtol

strtol

Example: strtol Data Type Conversion Functions

Converts a string to a long integer using a given radix.

long strtol( const char *string, char **endptr, int radix );

string String representing an integer number. The number is considered until a non-numeric character is found . Use the following format:
[whitespaces][+|-][0|0x][nnnnn]
(where whitespaces are tab or space character and nnnnn is a sequence of valid numbers following the specified radix)
endptr An address of a pointer.
radix Numeral radix in which the number to be interpreted. Must be 0 or be between 2 and 36. If it is 0 the radix of the string is determined by the initial characters of the string: 0x for hex, 0 for octal, and 1-9 for decimal.

strtol scans the initial portion of the string only, by stopping at the first non-numerical character. Any preceding white spaces are stripped. endptr points to the character that stopped the scan.

Return Values

The converted long int value from the input string. If conversion would cause overflow the result is LONG_MAX or LONG_MIN. If an error occurs or no conversion can be made, returns 0.