wcstol()
is a built-in function defined in the <wchar.h>
header in C.
Here is the function protype:
long wcstol( const wchar_t * str, wchar_t ** str_end, int base );
wcstol()
is used to interpret a long integer value from a wide character string.
The function takes the following as input parameters:
str:
Pointer to a wide character string
str_end:
Double pointer to a wide character which is set to the next valid character after extracting the integer
base:
Base of the extracted integer
wcstol()
returns the corresponding integer value as interpreted from the string and converts it to the required base.
#include <wchar.h>#include <stddef.h>#include <stdio.h>int main(){// initialize the stringwchar_t str[] = L"21 Gun Salute!";// initialize the basewchar_t* end_str;// print the long integer value with the end string// with base 10long value = wcstol(str, &end_str, 10);printf("String: %S\n", str);printf("Integer value: %d\n", value);printf("End String = %S\n", end_str);return 0;}
We begin by initializing a wchar_t
string, str
, that contains an integer value. To declare a wide-character string, we prefix the string with the letter ‘L’.
Next, we declare an end_str
pointer. The end_str
pointer is made to point to the next valid character after extracting the long integer. In the example above, this character was space.
The long integer value returned by
wcstol()
is stored in thelong
variable,value
Free Resources