Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

strtoll — convert text to long long

strtoll reads an initial signed integer as long long. endptr and errno let you check the conversion result.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <stdlib.h>
long long int strtoll(const char *restrict nptr, char **restrict endptr, int base);

strtoll skips leading whitespace and accepts an optional + or -. base must be 0 or 2–36. 0 detects decimal, octal (0), hexadecimal (0x), or binary (0b). C23 also accepts 0b with base 2; production runtime support is not attested.

If endptr is not NULL, the function stores the address of the first unconverted character in *endptr. If no conversion takes place, the function returns 0 and, when endptr is not NULL, *endptr == nptr. Out-of-range input yields LLONG_MIN or LLONG_MAX and errno == ERANGE. Set errno = 0 before calling.

A result of 0 alone does not indicate failure. To reject any characters after the integer, pass a non-null endptr and check, in order, that a conversion took place, that there was no range error, and that **endptr == '\0'. The last check also rejects any trailing whitespace left unconverted. C23 permits digit separators (') in integer constants in source code, but strtoll uses separate conversion rules. In the C locale, when base is 0, those rules explicitly exclude integer suffixes and digit separators from the integer-constant form. For bases 2–36, the digit sequence does not include separators or a separate suffix component. Other locales may accept additional locale-specific forms, so do not assume that digit grouping separators are always rejected.

Examples

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    const char *text = "-4200";
    char *end;
    errno = 0;
    long long value = strtoll(text, &end, 10);

    if (end == text) {
        puts("no digits");
        return EXIT_FAILURE;
    }
    if (errno == ERANGE) {
        puts("out of range");
        return EXIT_FAILURE;
    }
    if (*end != '\0') {
        puts("trailing characters");
        return EXIT_FAILURE;
    }
    printf("%lld\n", value);
    return EXIT_SUCCESS;
}

Result

-4200

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources