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.
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.
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
The decimal equivalents of all numbers are 100 171 83 10
- Function Overloading vs Function Overriding in C++
- What happens when a virtual function is called inside a non-virtual function in C++
- div() function in C++
- fma() function in C++
- exp() function C++
- log() function in C++
- set insert() function in C++ STL
- set lower_bound() function in C++ STL
- set count() function in C++ STL
- map upper_bound() function in C++ STL
- map count() function in C++ STL
- set max_size() function in C++ STL
- set find() function in C++ STL
- set equal_range() function in C++ STL
- map emplace_hint() function in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.