In C, wcstol() converts wide text to long. It belongs to the wide-character tools declared in <wchar.h>.
Use wchar_t for wide characters and prefix wide literals with L, as in L"Hello". The examples below use simple C Programming Tutorial patterns and print wide strings with %ls.
Syntax
The following concrete call shows the argument shape before the formal declaration.
C
wcstol(L"42", &end, 10);
Call
What it does
wcstol(L"42", &end, 10)
Converts wide text to long in a typical call.
Include <wchar.h>. Add only the supporting headers actually used by your program.
Library declaration
long wcstol(const wchar_t *restrict nptr, wchar_t **restrict endptr, int base);
Parameters
Parameter
Description
nptr
Wide string containing the number to parse.
endptr
Receives the first unconverted wide character; pass NULL when it is not needed.
base
Number base from 2 to 36, or 0 to recognize a prefix automatically.
Return Value
Return Value
Description
Converted number
The parsed numeric value.
0 / range limit
No conversion or a range error; inspect endptr and errno.
C Examples
Example 1 — Parse a number
This example uses wcstol() to parse a number, then exposes the result so you can connect the call with its output.
C
#include <stdio.h>
#include <wchar.h>
int main(void) {
wchar_t *end;
long value = wcstol(L"42 rest", &end, 10);
wprintf(L"value read, rest = %ls\n", end);
return 0;
}
Output
value read, rest = rest
How It Works
Set up this case. The program prepares the exact input needed to parse a number.
Parse the numeric prefix.wcstol(L"42", &end, 10) converts the usable characters and leaves the stopping position in end.
Read the normal result. The displayed output shows the direct result of the call for this input.
Interpret the output.value read, rest = rest confirms the behavior demonstrated by this example.
Example 2 — Inspect endptr
This example uses wcstol() to inspect endptr, then exposes the result so you can connect the call with its output.
C
#include <stdio.h>
#include <wchar.h>
int main(void) {
wchar_t *end;
(void)wcstol(L"123xyz", &end, 10);
wprintf(L"stopped at %ls\n", end);
return 0;
}
Output
stopped at xyz
How It Works
Choose the edge case. This example changes the input so you can see how wcstol() behaves for inspect endptr.
Parse the numeric prefix.wcstol(L"42", &end, 10) converts the usable characters and leaves the stopping position in end.
Inspect the status or pointer. The program checks what the function returned before relying on output data.
Interpret the output.stopped at xyz confirms the behavior demonstrated by this example.
Example 3 — Detect invalid input
This example uses wcstol() to detect invalid input, then exposes the result so you can connect the call with its output.
Prepare a practical variation. The data is arranged specifically to detect invalid input.
Parse the numeric prefix.wcstol(L"42", &end, 10) converts the usable characters and leaves the stopping position in end.
Notice the safety rule. The example keeps the count, destination capacity, stream state, and terminator requirements visible.
Interpret the output.converted = no confirms the behavior demonstrated by this example.
Frequently Asked Questions
wcstol() converts wide text to long. It is part of the standard wide-character library.
Its parameters are nptr, endptr, base. nptr: Wide string containing the number to parse. endptr: Receives the first unconverted wide character; pass NULL when it is not needed. base: Number base from 2 to 36, or 0 to recognize a prefix automatically.
Converted number means the parsed numeric value. 0 / range limit means no conversion or a range error; inspect endptr and errno.
Include <wchar.h> for wcstol(). Include only the supporting headers used by your program, such as <stdio.h> for FILE or <stdarg.h> for va_list.
A returned zero does not by itself prove failure. Use endptr to see whether any characters were consumed, and check errno for range errors.