STRTOUL(3) | Library Functions Manual | STRTOUL(3) |

`strtoul`

,
`strtoull`

,
`strtoumax`

,
`strtouq`

—
convert a string to an unsigned long, unsigned long
long or uintmax_t integer

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

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

`strtoul`

(`strtoull`

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

`strtoumax`

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

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

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

`strtouq`

(`strtoul`

() function converts the string in
`strtoull`

() function converts the string in
`strtoumax`

() function converts the string in
`strtouq`

() function is a deprecated
equivalent of `strtoull`

() 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 an
`A`

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

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

’ representing 35.)
If `strtoul`

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

() stores
the original value of `\0`

’ but
`\0`

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

(),
`strtoull`

(),
`strtoumax`

() and
`strtouq`

() functions return either the
result of the conversion or, if there was a leading minus sign, the negation
of the result of the conversion, unless the original (non-negated) value would
overflow. If overflow occurs, `strtoul`

()
returns `ULONG_MAX`

,
`strtoull`

() returns
`ULLONG_MAX`

,
`strtoumax`

() returns
`UINTMAX_MAX`

,
`strtouq`

() returns
`ULLONG_MAX`

and the global variable
`ERANGE`

.
There is no way to determine if `strtoul`

() has
processed a negative number (and returned an unsigned value) short of
examining the string in `EINVAL`

.
`strtoul`

(),
and the return value of `strtoul`

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

- [
`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.

`strtoul`

(),
`strtoull`

(), and
`strtoumax`

() 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 `strtouq`

() function is a
BSD extension and is provided for backwards
compatibility with legacy programs.
November 30, 2014 | OpenBSD-6.1 |