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

CC++Programming

atol() Function

The function atol() converts string into a long integer. It returns zero, when no conversion is performed. It returns the converted long int value.

Here is the syntax of atol in C++ language,

long int atol(const char *string)

Here is an example of atol() in C++ language,

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   long int a;
   char str[20] = "538756";
   a = atol(str);
   cout << "Converted string into long int : " << a << endl;
   return 0;
}

Output

Converted string into long int : 538756

atoll() Function

The function atoll() converts string into a long long integer. It returns zero, when no conversion is performed. It returns the converted long long int value.

Here is the syntax of atol in C++ language,

long long int atoll(const char *string)

Here is an example of atol() in C++ language,

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   long long int a;
   char str[20] = "349242974200";
   a = atoll(str);
   cout << "Converted string into long long int : " << a << endl;
   return 0;
}

Output

Converted string into long long int : 349242974200

atof() Function

The function atof() converts string into a floating point number of double type. It returns zero, when no conversion is performed. It returns converted floating point value.

Here is the syntax of atol in C++ language,

double atof(const char *string)

Here is an example of atof() in C++ language,

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   double a;
   char s[20] = "3492.42974200";
   a = atof(s);
   cout << "Converted string into floating point value : " << a << endl;
   return 0;
}

Output

Converted string into floating point value : 3492.43
raja
Published on 08-Oct-2018 11:31:33
Advertisements