In C/C++, atol(), atoll(), and atof() are functions used to convert strings to numbers of different types. These functions are Standard Library functions. In this article, we will learn these String-to-number conversion functions in C/C++.
1. atol() in C
The atol() function converts a C-style string (array of characters), passed as an argument to atol() function, to a long integer. It converts the C-string str to a value of type long int by interpreting the characters of the string as numerical values. It discards the leading whitespace characters until a non-whitespace character is found.
Syntax
long int atol ( const char * str );
Parameters
- The function accepts one mandatory parameter str which represents an integral number.
Return Value
- The function returns the long int representation of the string.
- It returns ‘0’ if no valid conversion can be performed.
Note: If the C-string str passed to atol() function is either empty or contains only whitespace characters, it is not a valid integral number, no conversion will be performed and it will return zero.
Example: Program to Illustrate the Working of atol() Function.
C
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str1[] = "5672345" ;
long int num1 = atol (str1);
printf ( "Number is %ld\n" , num1);
char str2[] = "10000002 0" ;
long int num2 = atol (str2);
printf ( "Number is %ld\n" , num2);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
char str1[] = "5672345" ;
long int num1 = atol (str1);
cout << "Number is " << num1 << "\n" ;
char str2[] = "10000002 0" ;
long int num2 = atol (str2);
cout << "Number is " << num2 << "\n" ;
return 0;
}
|
Output
Number is 5672345
Number is 10000002
2. atoll() in C
The atoll() function converts a C-style string, passed as an argument to atol() function, to a long long integer. It converts the C-string str to a value of type long long int by interpreting the characters of the string as numerical values. It discards the leading whitespace characters until a non-whitespace character is found.
Syntax
long long int atoll ( const char * str );
Parameters
- The function accepts a mandatory parameter str which is the representation of an integral number.
Return Value
- The function returns the long long int representation of the string.
- It returns ‘0’ if no valid conversion can be performed.
Note: If the C-string str passed to atol() function is either empty or contains only whitespace characters, it is not a valid integral number, no conversion will be performed and it will return zero.
Example: Program to Illustrate the Working of atoll() Function.
C
#include <stdio.h>
#include <stdlib.h>
int main()
{
char big_num1[] = "8239206483232728" ;
long long int num1 = atoll(big_num1);
printf ( "Number is %lld\n" , num1);
char big_num2[] = "100000 9 1324100" ;
long long int num2 = atoll(big_num2);
printf ( "Number is %lld\n" , num2);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
char big_num1[] = "8239206483232728" ;
long long int num1 = atoll(big_num1);
cout << "Number is " << num1 << "\n" ;
char big_num2[] = "100000 9 1324100" ;
long long int num2 = atoll(big_num2);
cout << "Number is " << num2 << "\n" ;
return 0;
}
|
Output
Number is 8239206483232728
Number is 100000
3. atof() in C
The atof() function converts a C-style string, passed as an argument to atol() function, to double. It converts the C-string str to a value of type double by interpreting the characters of the string as numerical values. It discards the leading whitespace characters until a non-whitespace character is found.
Syntax
double atof ( const char * str );
Parameters
- The function accepts a single mandatory parameter str which is the representation of a floating point number.
Return Value
- The function returns the double value type representation of the string.
- The function returns zero (0.0) if no conversion is performed.
Note: If the C-string str passed to atol() function is either empty or contains only whitespace characters, it is not a valid floating point number, no conversion will be performed and it will return 0.0.
Example: Program to Illustrate the Working of atof() Function.
C
#include <stdio.h>
#include <stdlib.h>
int main()
{
char pi[] = "3.1415926535" ;
double pi_val = atof (pi);
printf ( "Value of pi = %lf\n" , pi_val);
char acc_g[] = "9.8" ;
double acc_g_val = atof (acc_g);
printf ( "Value of acceleration due to gravity = %.1lf\n" ,
acc_g_val);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
char pi[] = "3.1415926535" ;
double pi_val = atof (pi);
cout << "Value of pi = " << pi_val << "\n" ;
char acc_g[] = "9.8" ;
double acc_g_val = atof (acc_g);
cout << "Value of acceleration due to gravity = "
<< acc_g_val << "\n" ;
return 0;
}
|
Output
Value of pi = 3.14159
Value of acceleration due to gravity = 9.8
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
06 Oct, 2023
Like Article
Save Article