Unlocking strtoll's Potential: A Guide to String-to-Long Long Conversion in C
Arguments
int base
(optional): This specifies the base of the number in the string. Valid values are between 2 and 36, or 0 (which tellsstrtoll
to automatically detect the base). The default base is 10 (decimal).char **endptr
(optional): This is a pointer to a character pointer variable.strtoll
will store the address of the first character in the string that it couldn't convert into a number. You can use this to check how much of the string was successfully converted.const char *str
: This is a pointer to the string that you want to convert.
Return Value
long long int
: The function returns the converted value as a long long integer. If the conversion fails, it returns zero and sets the global variableerrno
to indicate the error.
Process
strtoll
skips any leading whitespace characters in the string.- It checks for an optional sign character (
+
or-
). - It starts converting the string from the first non-whitespace character. It continues converting characters until it encounters a character that is not a valid digit in the specified base.
- For each valid digit,
strtoll
subtracts the character's ASCII value from '0' (or 'A' - 10 for uppercase letters if the base is greater than 10) to get its numerical value. - It multiplies the previously converted value by the base and adds the new digit's value to get the final result.
- If the
endptr
argument is provided,strtoll
stores the address of the character that stopped the conversion process in the location pointed to byendptr
.
Example
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "12345";
char *endptr;
long long int value;
value = strtoll(str, &endptr, 10);
printf("Converted value: %lld\n", value); // Output: Converted value: 12345
printf("End pointer: %s\n", endptr); // Output: End pointer: 5
return 0;
}
In this example, the string "12345" is converted to a long long integer with a base of 10. The endptr
points to the character \0
(null terminator) after the converted digits.
- The
endptr
argument is useful for validating how much of the string was actually converted. - It's important to check the value of
errno
ifstrtoll
returns zero to determine if an error occurred during the conversion. strtoll
is more flexible thanatoi
because it can handle larger numbers (long long) and supports different bases.
Converting a hexadecimal string
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "FF00AA";
char *endptr;
long long int value;
value = strtoll(str, &endptr, 16);
if (errno == ERANGE) {
printf("Error: Value out of range\n");
} else if (endptr == str) {
printf("Error: No valid digits found\n");
} else {
printf("Converted value (hex): %llX\n", value);
printf("End pointer: %s\n", endptr);
}
return 0;
}
This code converts the hexadecimal string "FF00AA" to a long long integer. It also includes error handling to check for conversion failures using errno
and invalid input using endptr
.
Handling overflow and underflow
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main() {
char str1[] = "9999999999999999999"; // Too large for long long
char str2[] = "-18446744073709551617"; // Too small for long long (assuming signed)
char *endptr;
long long int value;
value = strtoll(str1, &endptr, 10);
if (errno == ERANGE && (value == LLONG_MAX || value == LLONG_MIN)) {
printf("Error: Value out of range for long long\n");
} else if (endptr == str1) {
printf("Error: No valid digits found\n");
} else {
printf("Converted value (str1): %lld\n", value);
}
value = strtoll(str2, &endptr, 10);
// Similar error handling for str2
return 0;
}
This code demonstrates how to handle overflow and underflow errors. It checks errno
and the returned value to see if it's the maximum or minimum possible value for a long long integer.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
char str[] = " -123 ";
char *endptr;
long long int value;
// Skip leading whitespaces
while (isspace(*str)) {
str++;
}
value = strtoll(str, &endptr, 10);
if (errno == ERANGE) {
printf("Error: Value out of range\n");
} else if (endptr == str) {
printf("Error: No valid digits found\n");
} else {
printf("Converted value (with whitespaces): %lld\n", value);
}
return 0;
}
strtol
- This is the most common alternative for converting strings to a smaller integer type,
long int
. It works similarly tostrtoll
but returns along int
and has a smaller range for the numbers it can handle.
sscanf
- This function offers more flexibility for parsing formatted input strings. It can handle various data types like integers, floats, and characters within the same string. However, it requires you to specify the format string that defines how to interpret the input string.
Custom parsing logic
- If you have a specific format for your string and only need to convert integers, you can write your own parsing logic using a loop. This approach gives you complete control over the parsing process but requires more code and might be less robust compared to standard library functions.
strtoull (C99 or later)
- This function is similar to
strtoll
but specifically designed for unsigned long long integers. It's useful if you know the input string represents a non-negative number.
- Flexibility
Do you need to parse more complex formatted strings or just convert simple integer representations? - Error handling
How important is error handling for invalid input? - Base
Do you need to handle different bases (e.g., decimal, hexadecimal) or just base 10 (decimal)? - Data type
Do you need a long long integer or a smaller integer type?
Function | Return Type | Base Support | Error Handling (built-in) | Flexibility |
---|---|---|---|---|
strtoll | long long int | 2-36 or 0 | errno | Basic |
strtol | long int | 2-36 or 0 | errno | Basic |
sscanf | (varies) | (varies) | N/A | High |
custom logic | (varies) | (custom) | N/A | Low |
strtoull (C99+) | unsigned long long int | 2-36 or 0 | errno | Basic |