C strtoull() Function | Convert String to Unsigned Long Long Integer

Introduction

The strtoull() function in C is a standard library function that converts a string to an unsigned long long integer. It is part of the C standard library (stdlib.h). This function is more versatile and robust than atol() or atoi() because it allows for error checking and supports different number bases.

strtoull() Function Syntax

The syntax for the strtoull() function is as follows:

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

Parameters:

  • str: A C string that contains the representation of an unsigned long long integer.
  • endptr: A pointer to a character pointer. If endptr is not NULL, strtoull() stores the address of the first invalid character in *endptr.
  • base: The base of the number represented by str. This can be any value from 2 to 36, or 0 to automatically detect the base.

Returns:

  • The function returns the converted unsigned long long integer value. If no valid conversion could be performed, it returns 0.

Examples

Converting a Simple String to Unsigned Long Long Integer

To demonstrate how to use strtoull() to convert a string to an unsigned long long integer, we will write a simple program.

Example

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

int main() {
    const char *str = "18446744073709551615";
    char *endptr;
    unsigned long long int num;

    // Convert string to unsigned long long integer
    num = strtoull(str, &endptr, 10);

    // Print the converted value
    printf("The converted value is: %llu\n", num);

    // Print the remaining part of the string
    if (*endptr != '\0') {
        printf("Remaining part of the string: %s\n", endptr);
    }

    return 0;
}

Output:

The converted value is: 18446744073709551615

Handling Invalid Input

This example shows how strtoull() behaves with invalid input.

Example

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

int main() {
    const char *str = "abc18446744073709551615";
    char *endptr;
    unsigned long long int num;

    // Convert string to unsigned long long integer
    num = strtoull(str, &endptr, 10);

    // Print the converted value
    printf("The converted value is: %llu\n", num);

    // Print the remaining part of the string
    if (*endptr != '\0') {
        printf("Remaining part of the string: %s\n", endptr);
    }

    return 0;
}

Output:

The converted value is: 0
Remaining part of the string: abc18446744073709551615

Converting Strings with Different Bases

This example shows how strtoull() can be used to convert strings with different number bases.

Example

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

int main() {
    const char *str = "1A3F";
    char *endptr;
    unsigned long long int num;

    // Convert hexadecimal string to unsigned long long integer
    num = strtoull(str, &endptr, 16);

    // Print the converted value
    printf("The converted value is: %llu\n", num);

    // Print the remaining part of the string
    if (*endptr != '\0') {
        printf("Remaining part of the string: %s\n", endptr);
    }

    return 0;
}

Output:

The converted value is: 6719

Real-World Use Case

Converting User Input to Unsigned Long Long Integer with Error Checking

In real-world applications, the strtoull() function can be used to convert user input, provided as a string, into an unsigned long long integer with proper error checking.

Example

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

int main() {
    char input[100];
    char *endptr;
    unsigned long long int value;

    // Prompt the user for input
    printf("Enter an unsigned long long integer: ");
    fgets(input, sizeof(input), stdin);

    // Convert input to unsigned long long integer
    value = strtoull(input, &endptr, 10);

    // Check for errors
    if (endptr == input) {
        printf("No valid conversion could be performed.\n");
    } else {
        // Print the converted value
        printf("You entered: %llu\n", value);

        // Print the remaining part of the string
        if (*endptr != '\0' && *endptr != '\n') {
            printf("Remaining part of the string: %s\n", endptr);
        }
    }

    return 0;
}

Output (example user input "12345678901234567890abc"):

Enter an unsigned long long integer: 12345678901234567890abc
You entered: 12345678901234567890
Remaining part of the string: abc

Conclusion

The strtoull() function is used for converting strings to unsigned long long integer values in C. It allows for precise and reliable conversion of numerical data stored as strings, with the added benefit of error handling and support for different number bases. Always handle invalid input scenarios to ensure robust applications.

Comments