C++ strtoll()

The strtoll() function in C++ interprets the contents of a string as an integral number of the specified base and return its value as a long 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 "201xz$"

Valid numeric part -> 201
First character after valid numeric part -> x

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

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

The strtoll() 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 long int value.

This function is defined in <cstdlib> header file.

strtoll() 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}.

strtoll() Return value

The strtoll() function returns:

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

Example 1: How strtoll() function works?

#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

int main()
{
	int base = 10;
	char numberString[] = "13.5ab_1x";
	char *end; 
	long long int number;
	
	number = strtoll(numberString, &end, base);
	cout << "String value = " << numberString << endl;
	cout << "Long long int value = " << number << endl;
	cout << "End String = " << end << endl;
	
	strcpy(numberString, "13");
	cout << "String value = " << numberString << endl;
	number = strtoll(numberString, &end, base);
	cout << "Long long int value = " << number << endl;
	if (*end) {
		cout << end;
	} else {
		cout << "Null pointer";
	}
	return 0;
}

When you run the program, the output will be:

String value = 13.5ab_1x
Long long int value = 13
End String = .5ab_1x
String value = 13
Long long int value = 13
Null pointer

A valid integer value for strtoll() 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.

Example 2: strtoll() function with different bases

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

int main()
{
	char *end;
	
	cout << "23ajz" << " to Long Long Int with base-7 = " << strtoll("23ajz", &end, 7) << endl;
	cout << "End String = " << end << endl << endl;
	
	cout << "23ajz" << " to Long Long Int with base-20 = " << strtoll("23ajz", &end, 20) << endl;
	cout << "End String = " << end << endl << endl;
	
	cout << "23ajz" << " to Long Long Int with base-36 = " << strtoll("23ajz", &end, 36) << endl;
	cout << "End String = " << end << endl << endl;
	
	return 0;
}

When you run the program, the output will be:

23ajz to Long Long Int with base-7 = 17
End String = ajz

23ajz to Long Long Int with base-20 = 17419
End String = z

23ajz to Long Long Int with base-36 = 3512879
End String =

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

In general, a valid integer argument for strtoll() 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 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: strtoll() function for leading whitespace and invalid conversion

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

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

	cout << "ax110.97" << " to Long Long Int with base-10 = " << strtoll("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 Long Int with base-11 = 307
End String = xbz

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

ax110.97 to Long 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: strtoll() function with base 0

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

int main()
{
	char *end;
	
	/* octal base */
	cout << "025x" << " to Long Long Int with base-0 = " << strtoll("025x", &end, 0) << endl;
	cout << "End String = " << end << endl << endl;
	
	/* hexadecimal base */
	cout << "0xf1x" << " to Long Long Int with base-0 = " << strtoll("0xf1x", &end, 0) << endl;
	cout << "End String = " << end << endl << endl;
	
	/* decimal base */
	cout << "15ab" << " to Long Long Int with base-0 = " << strtoll("15ab", &end, 0) << endl;
	cout << "End String = " << end << endl << endl;
	
	return 0;
}

When you run the program, the output will be:

025x to Long Long Int with base-0 = 21
End String = x

0xf1x to Long Long Int with base-0 = 241
End String = x

15ab to Long Long Int with base-0 = 15
End String = ab