strtoull() in C – Syntax, Use and Example

Write a C program to input string from user and convert string to unsigned long long using strtoull() library function. How to convert string to unsigned long long int using strtoull() library function in C programming?

strtoull() library function

strtoull() function converts string representation of integer to unsigned long long int type. It is defined in stdlib.h header function.

It is a part of C11 standard. Therefore you must have C11 supported compiler to use this function.

Syntax of strtoull() function

unsigned long long int strtoull(const char* str, char** endPtr, int base);
  • str is a pointer to constant character. str points to string representing an integer.
  • endPtr is pointer to character pointer. On success it points to first character after number otherwise points to NULL.
  • Last parameter is base of string input.
  • On success the function return converted integer as unsigned long long int type and set endPtr to point to first character after number. On failure it return 0 and set endPtr to point to NULL pointer.

It handles integer overflows efficiently and return ULONG_LONG_MAX on overflow. However,
for integer underflows its behaviour is undefined.

Example program to use strtoull() function

/**
 * C program to convert string to unsigned long long int using strtoull() library function.
 */

#include <stdio.h>
#include <stdlib.h>     // Used for strtoull()


int main()
{
    char number[30];
    char* endPtr;

    unsigned long long int bigNumber;
    int base;


    /* Input string representation of number from user. */
    printf("Enter any number: ");
    fgets(number, 30, stdin);


    /* Input base of input string */
    printf("Enter base: ");
    scanf("%d", &base);


    /* Convert string representation of number to unsigned long long */
    bigNumber = strtoull(number, &endPtr, base);


    /* endPtr points to NULL for failed conversions */
    if(*endPtr)
        printf("Unable to convert '%s' to base %d.", number, base);
    else
        printf("Converted unsigned long long int = %llu\n", bigNumber);

    return 0;
}

Output

Enter any number: 13012018165450
Enter base: 10
Converted unsigned long long int = 13012018165450

Enter any number: ffffac
Enter base: 16
Converted unsigned long long int = 16777132

Enter any number: 05545
Enter base: 8
Converted unsigned long long int = 2917

Enter any number: 01111111111011101011
Enter base: 2
Converted unsigned long long int = 524011

Enter any number: 18446744073709551615
Enter base: 10
Converted unsigned long long int = 18446744073709551615

Enter any number: 184467440737095516151099
Enter base: 10
Converted unsigned long long int = 18446744073709551615

Enter any number: -18446744073709551615
Enter base: 10
Converted unsigned long long int = 1

Enter any number: -184467440737095516159999
Enter base: 10
Converted unsigned long long int = 18446744073709551615

Enter any number: -99
Enter base: 10
Converted unsigned long long int = 18446744073709551517

Enter any number: -100
Enter base: 10
Converted unsigned long long int = 18446744073709551516

Happy coding 😉