Open In App
Related Articles

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

Improve Article
Improve
Save Article
Save
Like Article
Like

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++




// 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;
}

Output

Number 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++




// 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;
}

Output

Number 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++




// 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;
}

Output

Value of pi = 3.14159
Value of acceleration due to gravity = 9.8

Last Updated : 04 Jun, 2023
Like Article
Save Article
Similar Reads