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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

Output:
String value --> 101GeeksForGeeks
Long integer value --> 10
End String = GeeksForGeeks

Program With different bases :
Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.


Article Tags :
C
Practice Tags :