C wcstoul() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
wchar.h

Definition

In C, wcstoul() converts wide text to unsigned 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
wcstoul(L"42", &end, 10);
CallWhat it does
wcstoul(L"42", &end, 10)Converts wide text to unsigned long in a typical call.

Include <wchar.h>. Add only the supporting headers actually used by your program.

Library declaration
unsigned long wcstoul(const wchar_t *restrict nptr, wchar_t **restrict endptr, int base);

Parameters

ParameterDescription
nptrWide string containing the number to parse.
endptrReceives the first unconverted wide character; pass NULL when it is not needed.
baseNumber base from 2 to 36, or 0 to recognize a prefix automatically.

Return Value

Return ValueDescription
Converted numberThe parsed numeric value.
0 / range limitNo conversion or a range error; inspect endptr and errno.

C Examples

Example 1 — Parse a number

This example uses wcstoul() 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;
    unsigned long value = wcstoul(L"42 rest", &end, 10);
    wprintf(L"value read, rest = %ls\n", end);
    return 0;
}

How It Works

  1. Set up this case. The program prepares the exact input needed to parse a number.
  2. Parse the numeric prefix. wcstoul(L"42", &end, 10) converts the usable characters and leaves the stopping position in end.
  3. Read the normal result. The displayed output shows the direct result of the call for this input.
  4. Interpret the output. value read, rest = rest confirms the behavior demonstrated by this example.

Example 2 — Inspect endptr

This example uses wcstoul() 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)wcstoul(L"123xyz", &end, 10);
    wprintf(L"stopped at %ls\n", end);
    return 0;
}

How It Works

  1. Choose the edge case. This example changes the input so you can see how wcstoul() behaves for inspect endptr.
  2. Parse the numeric prefix. wcstoul(L"42", &end, 10) converts the usable characters and leaves the stopping position in end.
  3. Inspect the status or pointer. The program checks what the function returned before relying on output data.
  4. Interpret the output. stopped at xyz confirms the behavior demonstrated by this example.

Example 3 — Detect invalid input

This example uses wcstoul() to detect invalid input, then exposes the result so you can connect the call with its output.

C
#include <stdio.h>
#include <wchar.h>

int main(void) {
    const wchar_t *input = L"not-a-number";
    wchar_t *end;
    (void)wcstoul(input, &end, 10);
    wprintf(L"converted = %ls\n", end == input ? L"no" : L"yes");
    return 0;
}

How It Works

  1. Prepare a practical variation. The data is arranged specifically to detect invalid input.
  2. Parse the numeric prefix. wcstoul(L"42", &end, 10) converts the usable characters and leaves the stopping position in end.
  3. Notice the safety rule. The example keeps the count, destination capacity, stream state, and terminator requirements visible.
  4. Interpret the output. converted = no confirms the behavior demonstrated by this example.

Frequently Asked Questions

wcstoul() converts wide text to unsigned 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 wcstoul(). 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.

Next: C wcstoull() Function

Continue learning C wide-character functions.

Next tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful