Open In App

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In C/C++, atol(), atoll(), and atof() are functions used to convert strings to numbers of different types. These functions are Standard Library functions. In this article, we will learn these String-to-number conversion functions in C/C++.

1. atol() in C

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: Program to Illustrate the Working of atol() Function.

C




// C program to illustrate
// working of atol() function.
#include <stdio.h>
// Include this header for atol function
#include <stdlib.h>
 
int main()
{
    // char array of numbers
    char str1[] = "5672345";
 
    // Function calling to convert to a long int
    long int num1 = atol(str1);
 
    printf("Number is %ld\n", num1);
 
    // char array of numbers without spaces
    char str2[] = "10000002 0";
 
    // Function calling to convert to a long int
    long int num2 = atol(str2);
 
    printf("Number is %ld\n", num2);
 
    return 0;
}


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

2. atoll() in C

The atoll() 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: Program to Illustrate the Working of atoll() Function.

C




// C program to illustrate
// working of atol() function.
#include <stdio.h>
// Include this header for atoll function
#include <stdlib.h>
 
int main()
{
    // char array of numbers
    char big_num1[] = "8239206483232728";
 
    // Function calling to convert to a long long int
    long long int num1 = atoll(big_num1);
 
    printf("Number is %lld\n", num1);
 
    // char array of numbers without spaces
    char big_num2[] = "100000 9 1324100";
 
    // Function calling to convert to a long long int
    long long int num2 = atoll(big_num2);
 
    printf("Number is %lld\n", num2);
 
    return 0;
}


C++




// CPP program to illustrate
// working of atoll() 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

3. atof() in C

The atof() 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: Program to Illustrate the Working of atof() Function.

C




// C program to illustrate
// working of atof() function.
#include <stdio.h>
// Include this header for atof function
#include <stdlib.h>
 
int main()
{
    // char array
    char pi[] = "3.1415926535";
 
    // Calling function to convert to a double
    double pi_val = atof(pi);
 
    // prints the double value
    printf("Value of pi = %lf\n", pi_val);
 
    // 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
    printf("Value of acceleration due to gravity = %.1lf\n",
           acc_g_val);
 
    return 0;
}


C++




// CPP program to illustrate
// working of atof() 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 : 06 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads