Open In App

wcstol() function in C/C++

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:

Return value : The function returns two value as below:

Below programs illustrate the above function:
Program 1 :




// C++ program to illustrate
// wcstol() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialize the string
    wchar_t string[] = L"101GeeksForGeeks";
  
    // initialize the base
    int base = 3;
    wchar_t* endString;
  
    // print the long integer value with the end string
    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 :




// C++ program to illustrate
// wcstol() function
// with different bases
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialize the string
    wchar_t string[] = L"101GFG";
  
    // initialize the base
  
    wchar_t* endString;
  
    // print the long integer value with the end string
    // with base 2
    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";
  
    // print the long integer value with the end string
    // with base 5
    value = wcstol(string, &endString, 5);
    wcout << L"String value --> " << string << "\n";
    wcout << L"Long integer value --> " << value << "\n";
    wcout << L"End String = " << endString << "\n";
  
    // print the long integer value with the end string
    // with base 12
    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

Article Tags :