atol(), atoll() and atof() functions in C++

Hello everyone, in this tutorial, we will learn about the atol() , atoll() and atof() functions available in C++.

atol() function

atol() function is used to convert string value into the long int value. It returns long int value and if the conversion fails it returns zero.atol() function requires one string parameter which is to be converted into long int.

Syntax:

a           long int atol ( const char * str )

Code:

#include <bits/stdc++.h> 
using namespace std;
int main() {
   long int a;
   char str[] = "674562";
   a = atol(str);   //function to convert it into long int
   std::cout << "String converted into long int is : "<<a; 
   return 0;
}

Output:

String converted into long int is : 674562

atoll() funtion

atoll() function is used to convert string value into the long long int value. It returns long long int value and if the conversion fails it returns zero. atoll() function requires one string parameter which is to be converted into long long int.

Syntax:

a            long long int atoll ( const char * str )

Code:

#include <bits/stdc++.h> 

using namespace std;

int main() {
   long long int a;
   char str[] = "241242774278";
   
   a = atoll(str);     //function to convert into long long int
   
   cout << "String converted into long long int is :"<<a; 
   return 0;
}

Output:

String converted into long long int is :241242774278

atof() function

atof() function is used to convert string value into the floating point number of double type. It returns floating point number and if the conversion fails it returns zero. atof() function requires one string parameter which is to be converted into floating point number.

Syntax:

a          double atol ( const char * str )

Code:

#include <bits/stdc++.h> 

using namespace std;

int main() {
   double a;
   char str[] = "189.4567";

   a = atof(str);     //function to convert into double value

   cout << "String converted into floating point value is : "<<a; 
   return 0;
}

Output:

String converted into double is : 189.457

Leave a Reply

Your email address will not be published. Required fields are marked *