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.
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: 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.
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
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
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 =
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
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++
- div() function in C++
- log() function in C++
- exp() function C++
- fma() 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.