Quick links
Write a C program to input string from user and convert string to long long
using atoll()
library function. How to convert string to long long int
type using atoll()
library function in C programming?
atoll()
library function
atoll()
function converts string representation of integer to long long
type. It is defined in stdlib.h
header file.
It is a part of C11 standard. Therefore, you must have C11 supported compiler installed to use this function.
Syntax of atoll()
function
long long int atoll(const char* str);
- It accepts a pointer to constant character
str
. Which points to string representation of integer. - On success it return converted integer as
long long int
type. On failure it return0LL
.
Note: The function behaviour is undefined if given integer underflows or overflows
long long
range. To handle such situations use more reliable and cross-platform strtoll() library function.
Example program to use atoll()
function
/**
* C program to convert string to long long using atoll() library function.
*/
#include <stdio.h>
#include <stdlib.h> // Used for atoll()
int main()
{
char number[30];
long long int bigNum;
/* Input string representation of integer from user. */
printf("Enter any integer: ");
fgets(number, 30, stdin);
/* Convert string representation of number to integer */
bigNum = atoll(number);
/* Use %lld or %lli to print long long type */
printf("Converted long long int = %lld\n", bigNum);
return 0;
}
See all format specifiers in C programming.
Output
Enter any integer: 9223372036854775807 Converted long long int = 9223372036854775807 Enter any integer: -9223372036854775808 Converted long long int = -9223372036854775808 Enter any integer: 922337203685477580799 Converted long long int = 9223372036854775807 Enter any integer: -922337203685477580899999999 Converted long long int = -9223372036854775808 Enter any integer: 2018 Converted long long int = 2018 Enter any integer: error Converted long long int = 0
Happy coding 😉