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

  1. 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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Number is 5672345
    Number is 10000002
    
  2. 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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Number is 8239206483232728
    Number is 100000
    
  3. 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 atol ( 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

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


My Personal Notes arrow_drop_up

A Programmer and A Machine learning Enthusiast

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.