STRTOL(3) | Library Functions Manual | STRTOL(3) |

`strtol`

,
`strtoll`

,
`strtoimax`

,
`strtoq`

—
convert string value to a long, long long or intmax_t
integer

```
#include
<limits.h>
```

```
#include
<stdlib.h>
```

`strtol`

(`strtoll`

(```
#include
<inttypes.h>
```

`strtoimax`

(```
#include
<sys/types.h>
```

```
#include
<limits.h>
```

```
#include
<stdlib.h>
```

`strtoq`

(`strtol`

() function converts the string in
`strtoll`

() function converts the string in
`strtoimax`

() function
converts the string in `strtoq`

() function is a deprecated
equivalent of `strtoll`

() and is provided for
backwards compatibility with legacy programs. The conversion is done according
to the given `+`

’ or
‘`-`

’ sign. If
`0x`

’ prefix, and the number
will be read in base 16; otherwise, a zero
`0`

’, in which case it is
taken as 8 (octal).
The remainder of the string is converted to a
`A`

’ in either upper or lower case
represents 10, ‘`B`

’ represents 11, and so
forth, with ‘`Z`

’ representing 35.)
If `strtol`

() stores the address of the first
invalid character in `strtol`

() stores
the original value of `\0`

’ but
`\0`

’ on return, the entire string was
valid.)
`strtol`

(),
`strtoll`

(),
`strtoimax`

(), and
`strtoq`

() functions return the result of the
conversion. If overflow or underflow occurs,
`ERANGE`

and the function return value is as
follows:
Function |
underflow |
overflow |

`strtol` () |
`LONG_MIN` |
`LONG_MAX` |

`strtoll` () |
`LLONG_MIN` |
`LLONG_MAX` |

`strtoimax` () |
`INTMAX_MIN` |
`INTMAX_MAX` |

`strtoq` () |
`LLONG_MIN` |
`LLONG_MAX` |

`EINVAL`

.
`strtol`

(),
and the return value of `strtol`

() cannot be
used unambiguously to signal an error:
char *ep; long lval; ... errno = 0; lval = strtol(buf, &ep, 10); if (buf[0] == '\0' || *ep != '\0') goto not_a_number; if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) goto out_of_range;

`strtol`

() is being used instead of
atoi(3), error
checking is further complicated because the desired return value is an
char *ep; int ival; long lval; ... errno = 0; lval = strtol(buf, &ep, 10); if (buf[0] == '\0' || *ep != '\0') goto not_a_number; if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) || (lval > INT_MAX || lval < INT_MIN)) goto out_of_range; ival = lval;

- [
`EINVAL`

] - The value of
`base`was neither between 2 and 36 inclusive nor the special value 0. - [
`ERANGE`

] - The given string was out of range; the value converted has been clamped.

`strtol`

(),
`strtoll`

(), and
`strtoimax`

() functions conform to
ISO/IEC 9899:1999
(“ISO C99”). Setting
`EINVAL`

is an extension to that standard
required by IEEE Std 1003.1-2008
(“POSIX.1”).
The `strtoq`

() function is a
BSD extension and is provided for backwards
compatibility with legacy programs.
April 14, 2015 | OpenBSD-6.1 |