C strtoull() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<stdlib.h>

Definition

The C strtoull() function converts checked text to unsigned long long. It skips leading white space, parses the longest valid unsigned integer prefix, and exposes where parsing stopped through endptr. This makes validation possible.

This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. Success produces a value, moves endptr past at least one converted character, and leaves errno free of ERANGE. No digits means endptr equals str. Range failure sets errno to ERANGE and returns ULLONG_MAX; trailing input is visible at *endptr. The examples below move from the direct call to an edge condition and a practical use.

Syntax

Start with this concrete strtoull() call:

C
strtoull("9000", &end, 10)
CallWhat it does
strtoull("9000", &end, 10)Converts checked text to unsigned long long using the concrete values shown.

Include <stdlib.h>. Also include headers that declare any types, limits, locale tools, or I/O functions used around the call.

Library declaration
C
unsigned long long strtoull(const char *restrict str, char **restrict endptr, int base);

Parameters

ParameterDescription
strText containing the unsigned integer.
endptrReceives the address of the first unconverted character; may be NULL.
baseConversion base from 2 through 36, or 0 for prefix-based detection.

Return Value

Return ValueDescription
unsigned long long valueThe converted prefix.
ULLONG_MAXRange error is reported with errno set to ERANGE.

C Examples

Example 1 — Parse and inspect the endpoint

This complete program demonstrates a distinct part of the strtoull() contract and checks the result or precondition that matters for the scenario.

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

int main(void) {
    const char *text="900 items";
    char *end;
    unsigned long long value=strtoull(text, &end, 10);
    printf("%llu, rest=%s\n", value, end);
    return 0;
}

How It Works

  1. Step 1. Point at text containing a numeric prefix.
  2. Step 2. Ask the function to save its stopping position.
  3. Step 3. The converted prefix becomes the requested numeric type.
  4. Step 4. end identifies the unconverted suffix for validation.

Example 2 — Reject text with no digits

This complete program demonstrates a distinct part of the strtoull() contract and checks the result or precondition that matters for the scenario.

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

int main(void) {
    const char *text="not-a-number"; char *end;
    unsigned long long value=strtoull(text, &end, 10);
    if (end==text) puts("no digits");
    else printf("%llu\n", value);
    return 0;
}

How It Works

  1. Step 1. Keep the original pointer for comparison.
  2. Step 2. Attempt conversion while collecting end.
  3. Step 3. No valid numeric prefix advances the endpoint.
  4. Step 4. Equality with the starting pointer detects the failure.

Example 3 — Check a range error

This complete program demonstrates a distinct part of the strtoull() contract and checks the result or precondition that matters for the scenario.

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

int main(void) {
    const char *text="999999999999999999999999999999999999"; char *end;
    errno=0;
    unsigned long long value=strtoull(text, &end, 10);
    if (errno==ERANGE) puts("out of range");
    else printf("%llu\n", value);
    return 0;
}

How It Works

  1. Step 1. Reset errno immediately before conversion.
  2. Step 2. Parse a value beyond the destination range.
  3. Step 3. Check ERANGE after ensuring conversion was attempted.
  4. Step 4. Reject the saturated or infinite result instead of using it.

Frequently Asked Questions

strtoull() converts checked text to unsigned long long. It skips leading white space, parses the longest valid unsigned integer prefix, and exposes where parsing stopped through endptr. This makes validation possible.
Include <stdlib.h> before calling strtoull().
Success produces a value, moves endptr past at least one converted character, and leaves errno free of ERANGE. No digits means endptr equals str. Range failure sets errno to ERANGE and returns ULLONG_MAX; trailing input is visible at *endptr.
Set errno to 0 before the call, inspect endptr to require digits and reject trailing text, then check ERANGE. A leading minus sign is accepted by the grammar and converted using unsigned rules, so reject it explicitly when negative input is forbidden.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.

Next: system()

Continue through the C stdlib.h tutorials.

system() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful