Introduction
strtoll()
function in C is a standard library function that converts a string to a long long integer. It is part of the C standard library (stdlib.h
). This function is more versatile and robust than atoll()
because it allows for error checking and supports different number bases.strtoll() Function Syntax
The syntax for the strtoll()
function is as follows:
long long int strtoll(const char *str, char **endptr, int base);
Parameters:
str
: A C string that contains the representation of a long long integer.endptr
: A pointer to a character pointer. Ifendptr
is notNULL
,strtoll()
stores the address of the first invalid character in*endptr
.base
: The base of the number represented bystr
. This can be any value from 2 to 36, or 0 to automatically detect the base.
Returns:
- The function returns the converted long long integer value. If no valid conversion could be performed, it returns 0.
Examples
Converting a Simple String to Long Long Integer
To demonstrate how to use strtoll()
to convert a string to a long long integer, we will write a simple program.
Example
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *str = "123456789012345";
char *endptr;
long long int num;
// Convert string to long long integer
num = strtoll(str, &endptr, 10);
// Print the converted value
printf("The converted value is: %lld\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: 123456789012345
Handling Invalid Input
This example shows how strtoll()
behaves with invalid input.
Example
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *str = "abc1234567890";
char *endptr;
long long int num;
// Convert string to long long integer
num = strtoll(str, &endptr, 10);
// Print the converted value
printf("The converted value is: %lld\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: abc1234567890
Converting Strings with Different Bases
This example shows how strtoll()
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;
long long int num;
// Convert hexadecimal string to long long integer
num = strtoll(str, &endptr, 16);
// Print the converted value
printf("The converted value is: %lld\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 Long Long Integer with Error Checking
In real-world applications, the strtoll()
function can be used to convert user input, provided as a string, into a long long integer with proper error checking.
Example
#include <stdio.h>
#include <stdlib.h>
int main() {
char input[100];
char *endptr;
long long int value;
// Prompt the user for input
printf("Enter a long long integer: ");
fgets(input, sizeof(input), stdin);
// Convert input to long long integer
value = strtoll(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: %lld\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 "1234567890123456789abc"):
Enter a long long integer: 1234567890123456789abc
You entered: 1234567890123456789
Remaining part of the string: abc
Conclusion
The strtoll()
function is used for converting strings to 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
Post a Comment
Leave Comment