atoll() Function
The atoll()
function in C converts a numeric string into a long long integer value. It interprets the contents of a string as an integral number and returns the corresponding long long integer.
Syntax of atoll()
long long int atoll(const char *str);
Parameters
Parameter | Description |
---|---|
str | C-string containing the representation of an integral number. |
This function behaves similarly to atol()
but returns a value of type long long int
, allowing it to handle larger numbers. If the string does not represent a valid number, the function returns 0. Moreover, if the numeric value exceeds the representable range of a long long int
, the behavior is undefined. For better error handling in such cases, consider using strtoll()
.
Return Value
On success, the function returns the converted integral number as a long long int
. If no valid conversion can be performed, it returns 0.
Exceptions
If the converted value exceeds the range of a long long int
, the behavior is undefined. In such cases, using strtoll()
is recommended for robust error handling.
Examples for atoll()
Example: Converting a Simple Numeric String
This example demonstrates how to convert a basic numeric string into a long long integer using atoll()
.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *numStr = "1234567890";
long long int num = atoll(numStr);
printf("Converted number: %lld\n", num);
return 0;
}
Explanation:
- The program defines a string representing a numeric value.
atoll()
converts this string into a long long integer.- The converted number is then printed, demonstrating the successful conversion.
Output:
Converted number: 1234567890
Example: Handling an Invalid Numeric String
This example illustrates the behavior of atoll()
when the input string does not start with a valid numeric representation.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *numStr = "abc123";
long long int num = atoll(numStr);
printf("Converted number: %lld\n", num);
return 0;
}
Explanation:
- The program defines a string that begins with non-numeric characters.
atoll()
attempts to convert the string, but since the initial characters are not numeric, it returns 0.- This result is printed, demonstrating how the function handles invalid input.
Output:
Converted number: 0
Example: Converting a Large Numeric String Within Range
This example demonstrates converting a string representing a large number that is within the representable range of a long long int
.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *numStr = "9223372036854775807";
long long int num = atoll(numStr);
printf("Converted number: %lld\n", num);
return 0;
}
Explanation:
- The program defines a string representing the maximum value for a
long long int
. atoll()
converts the string to along long int
value.- The program prints the converted value, showing that large numbers within the valid range are correctly handled.
Output:
Converted number: 9223372036854775807