strtol() function in C++ STL Last Updated : 01 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report The strtol() function is a builtin function in C++ STL which converts the contents of a string as an integral number of the specified base and return its value as a long int. Syntax: strtol(s, &end, b) Parameters: The function accepts three mandatory parameters which are described as below: s: specifies the string which has the representation of an integral number.end: indicates where the conversion stopped, refers to an already allocated object of type char*. The value of the end is set by the function to the next character in s after the last valid character.It can also be a null pointer, in which case it is not used.b: specifies to the base of the integral value. Return Value: The function returns value of two types: If a valid conversion occurs, then a long int value is returned.If no valid conversion happens, then 0 is returned. Below programs illustrate the above function. Program 1: CPP // C++ program to illustrate the // strtol() function when decimal base #include <cstdlib> #include <cstring> #include <iostream> #include <string> using namespace std; int main() { int b = 10; char s[] = "6010IG_2016p"; char* end; long int n; n = strtol(s, &end, b); cout << "Number in String = " << s << endl; cout << "Number in Long Int = " << n << endl; cout << "End String = " << end << endl << endl; // the pointer to invalid // characters can be null strcpy(s, "47"); cout << "Number in String = " << s << endl; n = strtol(s, &end, b); cout << "Number in Long Int = " << n << endl; if (*end) { cout << end; } else { cout << "Null pointer"; } return 0; } Output:Number in String = 6010IG_2016p Number in Long Int = 6010 End String = IG_2016p Number in String = 47 Number in Long Int = 47 Null pointer Program 2: CPP // C++ program to illustrate the // strtol() function #include <cstdlib> #include <cstring> #include <iostream> using namespace std; int main() { char* end; cout << "489bc" << " to Long Int with base-4 = " << strtol("489bc", &end, 4) << endl; cout << "End String = " << end << endl; cout << "123s" << " to Long Int with base-11 = " << strtol("123s", &end, 11) << endl; cout << "End String = " << end << endl; cout << "56xyz" << " to Long Int with base-36 = " << strtol("56xyz", &end, 36) << endl; } Output:489bc to Long Int with base-4 = 0 End String = 489bc 123s to Long Int with base-11 = 146 End String = s 56xyz to Long Int with base-36 = 8722043 Program 3: CPP // C++ program to illustrate the // strtol() function when base is 0 #include <cstdlib> #include <iostream> using namespace std; int main() { char* end; // octal base cout << "312gfg" << " to Long Int with base-0 = " << strtol("312gfg", &end, 0) << endl; cout << "End String = " << end << endl << endl; // hexadecimal base cout << "0q15axtz" << " to Long Int with base-0 = " << strtol("0q15axtz", &end, 0) << endl; cout << "End String = " << end << endl << endl; // decimal base cout << "33ffn" << " to Long Int with base-0 = " << strtol("33ffn", &end, 0) << endl; cout << "End String = "; return 0; } Output:312gfg to Long Int with base-0 = 312 End String = gfg 0q15axtz to Long Int with base-0 = 0 End String = q15axtz 33ffn to Long Int with base-0 = 33 End String = Program 4 CPP // C++ program to illustrate the // strtol() function for invalid // conversions and leading whitespaces. #include <cstdlib> #include <iostream> using namespace std; int main() { char* end; cout << "22abcd" << " to Long Int with base-6 = " << strtol(" 22abcd", &end, 6) << endl; cout << "End String = " << end << endl << endl; cout << "114cd" << " to Long Int with base-2 = " << strtol(" 114cd", &end, 2) << endl; cout << "End String = " << end << endl << endl; cout << "e10.79" << " to Long Int with base-10 = " << strtol("e10.79", &end, 10) << endl; cout << "End String = " << end << endl << endl; return 0; } Output:22abcd to Long Int with base-6 = 14 End String = abcd 114cd to Long Int with base-2 = 3 End String = 4cd e10.79 to Long Int with base-10 = 0 End String = e10.79 Comment More infoAdvertise with us Next Article std::stoi Function in C++ I IshwarGupta Follow Improve Article Tags : Misc C++ STL CPP-Library CPP-Functions +1 More Practice Tags : CPPMiscSTL Similar Reads 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 characte 3 min read std::stoi Function in C++ The stoi() is a standard library function that turns a string into an integer. C++ programmers utilize the function, which stands for "string to integer," to obtain integers from strings. Additionally, the stoi() function can remove other components, such as trailing letters from the string. Syntax: 3 min read strtod() function in C/C++ The strtod() is a builtin function in C and C++ STL which interprets the contents of the string as a floating point number and return its value as a double. It sets a pointer to point to the first character after the last valid character of the string, only if there is any, otherwise it sets the poi 4 min read logb() function in C++ STL The logb() is a builtin function in C++ STL which returns the logarithm of |x|, using FLT_RADIX as base for the logarithm. In general, the value of FLT_RADIX is 2, so logb() is equivalent to log2()(for positive values only). Syntax: logb(val) Parameter: The function accepts a single mandatory parame 2 min read strcat() Function in C++ The strcat() function in C++ is a predefined function in the <cstring> header file that is used to concatenate two strings, by appending a copy of the source string to the end of the destination string. This function works by adding all the characters till the null character of the source stri 2 min read scalbln() function in C++ STL The scalbln() is a built-in function in C++ STL which takes two arguments and scales x by FLT_RADIX raised to the power n. The function returns the product of x and FLT_RADIX raised to the power n. FLT_RADIX: It is the value of the radix (integer base) of the exponent representation. Syntax: scalbln 2 min read Like