strol() function in C++

The strtol() function in C++ interprets the contents of a string as an integral number of the specified base and return its value as a long int.This function also sets an end pointer that points to the first character after the last valid numeric character of the string, if there is no such character then the pointer is set to null. This function is defined in cstdlib header file.

Syntax:

long int strtol(const char* str, char** end, int base)

Parameter :The function acceps three mandatory parameters which are described below.



  • str: A string consist of an integral number.
  • end: This is reference to object of type char*. The value of end is set by the function to the next character in str after the last valid numeric character. This parameter can also be a null pointer, in case if it is not used.
  • base: It represent the numerical base (radix) that determines the valid characters and their interpretation in the string

Return type :The function returns two types of values which are described below:

  • If valid conversion occur then the function returns the converted integral number as long int value.
  • If no valid conversion could be performed, a zero value is returned.

Below programs illustrate the above function.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// strtol() function
#include <bits/stdc++.h>
using namespace std;
  
// Driver code
int main()
{
    int base = 10;
    char str[] = "123abc";
    char* end;
    long int num;
  
    // Function used to convert string
    num = strtol(str, &end, base);
  
    cout << "Given  String = " << str << endl;
    cout << "Number with base 10 in string " << num << endl;
    cout << "End String points to " << end << endl
         << endl;
  
    // in this case the end pointer poijnts to null
    strcpy(str, "12345");
  
    // prints the current string
    cout << "Given String = " << str << endl;
  
    // function used
    num = strtol(str, &end, base);
  
    // prints the converted integer
    cout << "Number with base 10 in string " << num << endl;
    if (*end) {
        cout << end;
    }
    else {
        cout << "Null pointer";
    }
    return 0;
}

chevron_right


Output:

Given  String = 123abc
Number with base 10 in string 123
End String points to abc

Given String = 12345
Number with base 10 in string 12345
Null pointer

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// strtol() function Program to
// convert multiple values at different base
#include <bits/stdc++.h>
using namespace std;
  
// Drriver code
int main()
{
    char str[] = "100 ab 123 1010";
    char* end;
    long int a, b, c, d;
  
    // base 10
    a = strtol(str, &end, 10);
  
    // base 16
    b = strtol(end, &end, 16);
  
    // base 8
    c = strtol(end, &end, 8);
  
    // base 2
    d = strtol(end, &end, 2);
  
    cout << "The decimal equivalents of all numbers are \n";
  
    cout << a << endl
         << b << endl
         << c << endl
         << d;
    return 0;
}

chevron_right


Output:

The decimal equivalents of all numbers are 
100
171
83
10


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.