C++ strtol()

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.

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 a pointer to point to the first character after the last valid character of the string if there is any, otherwise the pointer is set to null.

For base 10 and the string "12abc":
Valid numeric part -> 12
First character after valid numeric part -> a

strtol() prototype [As of C++ 11 standard]

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

The strtol() function takes string, a pointer to character and an integer value - base as its parameter, interprets the content of string as an integral number of the given base and returns a long int value.

This function is defined in <cstdlib> header file.


strtol() Parameters

  • str: A string having the representation of an integral number.
  • end: Reference to an already allocated object of type char*. The value of end is set by the function to the next character in str after the last valid character. This parameter can also be a null pointer, in which case it is not used.
  • base: The base of the integral value. The set of valid values for base is {0, 2, 3, …, 35, 36}.

strtol() Return value

The strtol() function returns:

  • a long int value (which is converted from the string).
  • 0 if no valid conversion could be performed.

Example 1: How strtol() works in C++?

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int base = 10;
    char str[] = "27ab_1x";
    char *end; 
    long int num;
    
    num = strtol(str, &end, base);
    cout << "Number in  String = " << str << endl;
    cout << "Number in Long Int = " << num << endl;
    cout << "End String = " << end << endl << endl;
    
    // the pointer to invalid characters can be null
    strcpy(str, "27");
    cout << "Number in  String = " << str << endl;
    num = strtol(str, &end, base);
    cout << "Number in Long Int = " << num << endl;
    if (*end) {
        cout << end;
    } else {
        cout << "Null pointer";
    }
    return 0;
}

When you run the program, the output will be:

Number in  String = 27ab_1x
Number in Long Int = 27
End String = ab_1x

Number in  String = 27
Number in Long Int = 27
Null pointer

A valid integer value for strtol() function consists of:

  • An optional + or - sign.
  • A prefix 0 for octal base (applies only when base = 8 or 0).
  • A prefix 0x or 0X for hexadecimal base (applies only when base = 16 or 0).
  • A sequence of digits and/or alphabets (if base is greater than 10).

The valid values for parameter base is {0, 2, 3, ..., 35, 36}. A set of valid digits for base 2 is {0, 1}, for base 3 is {0, 1, 2} and so on. For bases starting from 11 to 36, valid digits include alphabets. The set of valid digits for base 11 is {0, 1, …, 9, A, a}, for base 12 is {0, 1, …, 9, A, a, B, b} and so on.

Note: It is important to remember that a valid character for one base can end up in the invalid string for another base as in the example below.

Example 2: strtol() function with different bases

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

int main()
{
    char *end;
    
    cout << "128bz" << " to Long Int with base-5 = " << strtol("128bxz", &end, 5) << endl;
    cout << "End String = " << end << endl << endl;
    
    cout << "128bz" << " to Long Int with base-12 = " << strtol("128bxz", &end, 12) << endl;
    cout << "End String = " << end << endl << endl;
    
    cout << "128bz" << " to Long Int with base-36 = " << strtol("128bxz", &end, 36) << endl;
    cout << "End String = " << end << endl << endl;
    
    return 0;
}

When you run the program, the output will be:

128bz to Long Int with base-5 = 7
End String = 8bxz

128bz to Long Int with base-12 = 2123
End String = xz

128bz to Long Int with base-36 = 64214135
End String =

The strtol() function ignores all the leading whitespace characters until the primary non-whitespace character is found.

In general, a valid integer argument for strtol() function has the following form:

[whitespace] [- | +] [0 | 0x] [alphanumeric characters]

Then, beginning from this character, it takes as many characters as possible that forms a valid integer representation and converts them to a long int value. Whatever is left of the string after the last valid character is ignored and has no effect on the result.

Example 3: strtol() function for leading whitespace and invalid conversion

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    char *end;
    
    cout << "  25axbz" << " to Long Int with base-11 = " << strtol("  25axbz", &end, 11) << endl;
    cout << "End String = " << end << endl << endl;
    
    cout << "   110bcd" << " to Long Int with base-2 = " << strtol("   110bcd", &end, 2) << endl;
    cout << "End String = " << end << endl << endl;

    cout << "ax110.97" << " to Long Int with base-10 = " << strtol("ax110.97", &end, 10) << endl;
    cout << "End String = " << end << endl << endl;

    return 0;
}

When you run the program, the output will be:

  25axbz to Long Int with base-11 = 307
End String = xbz

   110bcd to Long Int with base-2 = 6
End String = bcd

ax110.97 to Long Int with base-10 = 0
End String = ax110.97

If the base is ​0​, the numeric base is determined automatically by looking at the format of the string. If the prefix is 0, the base is octal (8). If the prefix is 0x or 0X, the base is hexadecimal (16), otherwise the base is decimal (10).

Example 4: strtol() function with base 0

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    char *end;
    
    /* octal base */
    cout << "0128ai" << " to Long Int with base-0 = " << strtol("0128ai", &end, 0) << endl;
    cout << "End String = " << end << endl << endl;
    
    /* hexadecimal base */
    cout << "0x15axzz" << " to Long Int with base-0 = " << strtol("0x15axzz", &end, 0) << endl;
    cout << "End String = " << end << endl << endl;
    
    /* decimal base */
    cout << "23dfl" << " to Long Int with base-0 = " << strtol("23dfl", &end, 0) << endl;
    cout << "End String = " << end << endl << endl;
    
    return 0;
}

When you run the program, the output will be:

0128ai to Long Int with base-0 = 10
End String = 8ai

0x15axzz to Long Int with base-0 = 346
End String = xzz

23dfl to Long Int with base-0 = 23
End String = dfl