atol(), atoll() and atof() functions in C/C++
- 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>usingnamespacestd;intmain(){// char array of numberscharstr1[] ="5672345";// Function calling to convert to a long intlongintnum1 =atol(str1);cout <<"Number is "<< num1 <<"\n";// char array of numbers of spacescharstr2[] ="10000002 0";// Function calling to convert to a long intlongintnum2 =atol(str2);cout <<"Number is "<< num2 <<"\n";return0;}Output: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>usingnamespacestd;intmain(){// char array of numberscharbig_num1[] ="8239206483232728";// Function calling to convert to a long intlonglongintnum1 = atoll(big_num1);cout <<"Number is "<< num1 <<"\n";// char array of numbers of spacescharbig_num2[] ="100000 9 1324100";// Function calling to convert to a long intlonglongintnum2 = atoll(big_num2);cout <<"Number is "<< num2 <<"\n";return0;}Output: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>usingnamespacestd;intmain(){// char arraycharpi[] ="3.1415926535";// Calling function to convert to a doubledoublepi_val =atof(pi);// prints the double valuecout <<"Value of pi = "<< pi_val <<"\n";// char arraycharacc_g[] ="9.8";// Calling function to convert to a doubledoubleacc_g_val =atof(acc_g);// prints the double valuecout <<"Value of acceleration due to gravity = "<< acc_g_val <<"\n";return0;}Output:Value of pi = 3.14159 Value of acceleration due to gravity = 9.8



Please Login to comment...