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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Difference between user defined function and library function in C/C++
- Function Overloading vs Function Overriding in C++
- How to call some function before main() function in C++?
- How to call function within function in C or C++
- What happens when a virtual function is called inside a non-virtual function in C++
- Difference between Virtual function and Pure virtual function in C++
- log() function in C++
- exp() function C++
- fma() function in C++
- div() function in C++
- fegetexceptflag() function in C/C++
- fegetenv() function in C/C++
- wctrans() function in C/C++
- wmemcpy() function in C/C++
- wcsncmp() function in C/C++
- wcsrtombs() function in C/C++
- wcstod() function in C/C++
- wcstol() function in C/C++
- wcsncat() function in C/C++
- wcstoul() function in C/C++
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.