atol()
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
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;
}
|
OutputNumber is 5672345
Number is 10000002
atoll()
This 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
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;
}
|
OutputNumber is 8239206483232728
Number is 100000
atof()
This 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
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;
}
|
OutputValue of pi = 3.14159
Value of acceleration due to gravity = 9.8