wcstol() function in C/C++
Last Updated :
17 Sep, 2018
The wcstol() function in C/C++ converts the given wide string to a long integer. This function sets a pointer to point to the first character after the last valid character of the wide string if there is any, otherwise, the pointer is set to null. This function ignores all the leading whitespace characters until the primary non-whitespace character is found.
Syntax:
long int wcstol( const wchar_t* str, wchar_t** endString, int base )
Parameters: The function accepts three mandatory parameters which are described below:
- string : String beginning with an integral form.
- endString : This is set by the function to the next character in string after the last valid character.
- base : The set of valid values for base is {0, 2, 3, …, 35, 36}.
Return value : The function returns two value as below:
- On success, the function returns the converted integral number as a long int value.
- Returns zero, if no valid conversion could be performed.
- If the value read is out of the range of representable values by a long int, the function returns LONG_MAX or LONG_MIN (defined in ), and errno is set to ERANGE.
Below programs illustrate the above function:
Program 1 :
#include <bits/stdc++.h>
using namespace std;
int main()
{
wchar_t string[] = L "101GeeksForGeeks" ;
int base = 3;
wchar_t * endString;
long value = wcstol(string, &endString, base);
wcout << L "String value --> " << string << "\n" ;
wcout << L "Long integer value --> " << value << "\n" ;
wcout << L "End String = " << endString << "\n" ;
return 0;
}
|
Output:
String value --> 101GeeksForGeeks
Long integer value --> 10
End String = GeeksForGeeks
Program With different bases :
Program 2 :
#include <bits/stdc++.h>
using namespace std;
int main()
{
wchar_t string[] = L "101GFG" ;
wchar_t * endString;
long value = wcstol(string, &endString, 2);
wcout << L "String value --> " << string << "\n" ;
wcout << L "Long integer value --> " << value << "\n" ;
wcout << L "End String = " << endString << "\n" ;
value = wcstol(string, &endString, 5);
wcout << L "String value --> " << string << "\n" ;
wcout << L "Long integer value --> " << value << "\n" ;
wcout << L "End String = " << endString << "\n" ;
value = wcstol(string, &endString, 12);
wcout << L "String value --> " << string << "\n" ;
wcout << L "Long integer value --> " << value << "\n" ;
wcout << L "End String = " << endString << "\n" ;
return 0;
}
|
Output:
String value --> 101GFG
Long integer value --> 5
End String = GFG
String value --> 101GFG
Long integer value --> 26
End String = GFG
String value --> 101GFG
Long integer value --> 145
End String = GFG
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...