- atol(): This function converts a C-type string, passed as an argument to function call, to a long integer. It parses the C-string str interpreting its content as an integral number, which is returned as a value of type long int. The function discards the whitespace characters present at the beginning of the string until a non-whitespace character is found. If the sequence of non-whitespace characters in C-string str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.
Syntax:
long int atol ( const char * str )
Parameters: The function accepts one mandatory parameter str which is the representation of an integral number.
Return Value: The function returns the converted integral number as a long int. If no valid conversion can be performed, it returns zero.
// CPP program to illustrate
// working of atol() function.
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
// char array of numbers
char
str1[] =
"5672345"
;
// Function calling to convert to a long int
long
int
num1 =
atol
(str1);
cout <<
"Number is "
<< num1 <<
"\n"
;
// char array of numbers of spaces
char
str2[] =
"10000002 0"
;
// Function calling to convert to a long int
long
int
num2 =
atol
(str2);
cout <<
"Number is "
<< num2 <<
"\n"
;
return
0;
}
chevron_rightfilter_noneOutput:Number is 5672345 Number is 10000002
- atoll(): This function converts a C-type string, passed as an argument to function call, to a long long integer. It parses the C-string str interpreting its content as an integral number, which is returned as a value of type long long int. The function discards the whitespace characters present at the beginning of the string until a non-whitespace character is found.
If the sequence of non-whitespace characters in C-string str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.
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 converted integral number as a long long int. If no valid conversion can be performed, it returns zero.
// CPP program to illustrate
// working of atol() function.
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
// char array of numbers
char
big_num1[] =
"8239206483232728"
;
// Function calling to convert to a long int
long
long
int
num1 = atoll(big_num1);
cout <<
"Number is "
<< num1 <<
"\n"
;
// char array of numbers of spaces
char
big_num2[] =
"100000 9 1324100"
;
// Function calling to convert to a long int
long
long
int
num2 = atoll(big_num2);
cout <<
"Number is "
<< num2 <<
"\n"
;
return
0;
}
chevron_rightfilter_noneOutput:Number is 8239206483232728 Number is 100000
- atof() function: This function converts a C-type string, passed as an argument to function call, to double. It parses the C-string str interpreting its content as a floating point number, which is returned as a value of type double. The function discards the whitespace characters present at the beginning of the string until a non-whitespace character is found. If the sequence of non-whitespace characters in C-string str is not a valid floating point number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and 0.0 is returned.
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 converted floating point number as a double value. If no valid conversion can be performed, the function returns zero (0.0).
Return Value:
// CPP program to illustrate
// working of atol() function.
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
// char array
char
pi[] =
"3.1415926535"
;
// Calling function to convert to a double
double
pi_val =
atof
(pi);
// prints the double value
cout <<
"Value of pi = "
<< pi_val <<
"\n"
;
// char array
char
acc_g[] =
"9.8"
;
// Calling function to convert to a double
double
acc_g_val =
atof
(acc_g);
// prints the double value
cout <<
"Value of acceleration due to gravity = "
<< acc_g_val <<
"\n"
;
return
0;
}
chevron_rightfilter_noneOutput:Value of pi = 3.14159 Value of acceleration due to gravity = 9.8
Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.