In the C Programming Language, the strtoll function converts a string to a lengthy long.
The strtoll function skips all white-space characters at the opening of the string, converts the subsequent characters as phase of the number, and then stops when it encounters the first character that is not a number.
Syntax
The syntax for the strtoll characteristic in the C Language is:
long long strtoll(const char *nptr, char **endptr, int base);
Parameters or Arguments
nptr A pointer to a string to convert to a long integer. endptr It is used by the strtoll characteristic to indicate where the conversion stopped. The strtoll feature will adjust endptr (if endptr is now not a null pointer) so that endptr points to the first personality that was no longer converted. base It is the base of the number being converted. If base is between 2 and 36, it is used as the radix of the number. If base is zero, the wide variety is assumed to be decimal except the transformed wide variety starts with O (for Octal), Ox (for hex) or OX (for hex).
Returns
The strtoll feature returns the lengthy long representation of a string. The strtoll function skips all white-space characters at the opening of the string, converts the subsequent characters as part of the number, and then stops when it encounters the first personality that is not a number.
If the strtoll characteristic converts a value that is too massive or too small to convert, it will save ERANGE in errono. If the fee is too large to convert, the feature will return a price of LLONG_MAX. If the price is too small to convert the function will return a value of LLONG_MIN.
Required Header
In the C Language, the required header for the strtoll characteristic is:
#include <stdlib.h>
Applies To
In the C Language, the strtoll feature can be used in the following versions:
ANSI/ISO 9899-1990
strtoll Example
Let’s seem to be at an instance to see how you would use the strtoll characteristic in a C program:
/* Example using strtoll by TechOnTheNet.com */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
int main(int argc, const char * argv[])
{
/* Define temporary variables */
char value[10];
char *eptr;
long long result;
/* Copy a value into the variable */
/* It's okay to have whitespace before the number */
strcpy(value, " 234");
/* Convert the provided value to a decimal long long */
result = strtoll(value, &eptr, 10);
/* If the result is 0, test for an error */
if (result == 0)
{
/* If a conversion error occurred, display a message and exit */
if (errno == EINVAL)
{
printf("Conversion error occurred: %d\n", errno);
exit(0);
}
}
/* If the result is equal to LLONG_MIN or LLONG_MAX, test for a range error */
if (result == LLONG_MIN || result == LLONG_MAX)
{
/* If the value provided was out of range, display a warning message */
if (errno == ERANGE)
printf("The value provided was out of range\n");
}
/* Display the converted result */
printf("%lld decimal\n", result);
/* Copy a hexadecimal value into the variable */
strcpy(value, "0x2af");
/* Convert the provided value to a decimal long long */
result = strtoll(value, &eptr, 16);
/* If the result is 0, test for an error */
if (result == 0)
{
/* If a conversion error occurred, display a message and exit */
if (errno == EINVAL)
{
printf("Conversion error occurred: %d\n", errno);
exit(0);
}
}
/* If the result is equal to LLONG_MIN or LLONG_MAX, test for a range error */
if (result == LLONG_MIN || result == LLONG_MAX)
{
/* If the value provided was out of range, display a warning message */
if (errno == ERANGE)
printf("The value provided was out of range\n");
}
/* Display the converted result */
printf("%llx hexadecimal\n", result);
return 0;
}
When compiled and run, this software will output:
234 decimal
2af hexadecimal
Similar Functions
Other C functions that are comparable to the strtoll function:
atoi characteristic atol feature strtod feature strtol characteristic strtoul function
See Also
Other C features that are noteworthy when dealing with the strtoll function:
atof function <stdlib.h>
Leave a Review