strtoll() Function
The strtoll()
function is declared in the header file <stdlib.h>
.
The strtoll()
function converts a string representation of an integral number into a value of type long long int
. It interprets the input string based on the specified numerical base and can provide a pointer to the character where the conversion stopped, enabling further processing of the string.
Syntax of strtoll()
long long int strtoll(const char* str, char** endptr, int base);
Parameters
Parameter | Description |
---|---|
str | A C-string that begins with the representation of an integral number. |
endptr | A pointer to a pointer of char. After conversion, it is set to point to the character in str immediately after the numerical value. It can be NULL if this information is not needed. |
base | An integer representing the numerical base (radix) for conversion. If this is 0, the base is determined automatically based on the format of str . |
It is worth noting that strtoll()
behaves similarly to strtol()
, but it supports conversion into a larger integer type. The function handles various bases and can indicate conversion errors, such as out-of-range values or the absence of any valid conversion.
Return Value
On success, strtoll()
returns the converted value as a long long int
. If no valid conversion is performed, it returns 0LL. In cases where the converted value is outside the range of representable values, LLONG_MAX
or LLONG_MIN
is returned, and errno
is set to ERANGE
.
Examples for strtoll()
Example 1: Basic Decimal Conversion
This example demonstrates a simple conversion of a decimal string into a long long int
using base 10.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *numStr = "1234567890123";
char *end;
long long int num = strtoll(numStr, &end, 10);
printf("Converted number: %lld\n", num);
return 0;
}
Explanation:
- A string
"1234567890123"
is defined. strtoll()
converts this string to along long int
using base 10.- The result is stored in the variable
num
and printed.
Program Output:
Converted number: 1234567890123
Example 2: Hexadecimal Conversion
This example converts a hexadecimal string into a long long int
by specifying base 16.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *hexStr = "0x1A3F";
char *end;
long long int num = strtoll(hexStr, &end, 16);
printf("Converted hexadecimal: %lld\n", num);
return 0;
}
Explanation:
- A hexadecimal string
"0x1A3F"
is defined. strtoll()
is called with base 16 to convert the string.- The resulting number is printed as a
long long int
.
Program Output:
Converted hexadecimal: 6719
Example 3: Using endptr to Determine Conversion Stop Point
This example illustrates how to use the endptr
parameter to determine where the numerical conversion stopped in the input string.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *inputStr = "789xyz";
char *end;
long long int num = strtoll(inputStr, &end, 10);
printf("Converted number: %lld\n", num);
printf("Remaining string: %s\n", end);
return 0;
}
Explanation:
- The string
"789xyz"
contains a valid number followed by non-numeric characters. strtoll()
converts the numeric portion"789"
into along long int
.- The pointer
end
is set to the beginning of the remaining non-numeric characters ("xyz"
), which is then printed.
Program Output:
Converted number: 789
Remaining string: xyz
Example 4: Handling Invalid Conversion and Range Errors
This example demonstrates how strtoll()
handles a case where no valid conversion occurs and where the converted value exceeds the representable range.
Program
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
int main() {
const char *invalidStr = "not_a_number";
char *end;
errno = 0;
long long int num = strtoll(invalidStr, &end, 10);
if (invalidStr == end) {
printf("No valid conversion could be performed.\n");
} else if (errno == ERANGE) {
printf("The number is out of range.\n");
} else {
printf("Converted number: %lld\n", num);
}
return 0;
}
Explanation:
- An input string
"not_a_number"
that does not start with a valid numeric representation is defined. strtoll()
fails to perform a conversion, and theend
pointer remains at the start of the string.- The program checks if no conversion occurred and prints an appropriate message.
Program Output:
No valid conversion could be performed.