strtol is a function in the C programming language that converts a string into a long integer.
strtol stands for string to long. It is included in the C standard library header file
stdlib.h. Its prototype is as follows:
long strtol(const char *restrict str, char **restrict end, int base);
str argument points to a string, represented by an array of characters, containing the character representation of a signed integer value. The string must be null-terminated. The
base argument specifies the number base to use, from 2 to 36. If the number base is greater than 10, alphabetic characters (
'A' up to
'Z') are used as digits in the representation. After the conversion, the value pointed to by
end is set to point to the character following the last valid numeric character in the string and the converted integer value is returned. If the string does not contain a valid numerical sequence, zero (0) is returned and the global variable
errno is set to
There also exist similar variants of these function named
strtoull which parses and returns an integer of the type
long long, and
unsigned long long respectively.
The strtol function is part of the ISO standard C library (C89, 1989), and the strtoll function was added as part of the C99 library (1999). It was added to the standard C library as a more well-behaved replacement for the existing atoi function.