What is wcstol() in C?

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 );

Functionality

wcstol() is used to interpret a long integer value from a wide character string.

The wcstol() function

Parameters and return value

The function takes the following as input parameters:

  1. str: Pointer to a wide character string

  2. str_end: Double pointer to a wide character which is set to the next valid character after extracting the integer

  3. base: Base of the extracted integer

wcstol() returns the corresponding integer value as interpreted from the string and converts it to the required base.

Code

#include <wchar.h>
#include <stddef.h>
#include <stdio.h>
int main(){
// initialize the string
wchar_t str[] = L"21 Gun Salute!";
// initialize the base
wchar_t* end_str;
// print the long integer value with the end string
// with base 10
long 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 the long variable, value

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved