strtoul, strtoull

Header: <stdlib.h>

Interprets an unsigned integer value in a byte string pointed to by str.

# Declarations

unsigned long strtoul( const char *str, char **str_end,
int base );

(until C99)

unsigned long strtoul( const char *restrict str, char **restrict str_end,
int base );

(since C99)

unsigned long long strtoull( const char *restrict str, char **restrict str_end,
int base );

(since C99)

# Parameters

# Return value

Integer value corresponding to the contents of str on success. If the converted value falls out of range of corresponding return type, range error occurs (errno is set to ERANGE) and ULONG_MAX or ULLONG_MAX is returned. If no conversion can be performed, 0 is returned.

# Example

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    const char *p = "10 200000000000000000000000000000 30 -40 - 42";
    printf("Parsing '%s':\n", p);
    char *end = NULL;
    for (unsigned long i = strtoul(p, &end, 10);
         p != end;
         i = strtoul(p, &end, 10))
    {
        printf("'%.*s' -> ", (int)(end - p), p);
        p = end;
        if (errno == ERANGE)
        {
            errno = 0;
            printf("range error, got ");
        }
        printf("%lu\n", i);
    }
    printf("After the loop p points to '%s'\n", p);
}

# See also