C wcsxfrm() Function

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

Definition

In C, wcsxfrm() builds a locale collation key for a wide string. 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
wcsxfrm(key, L"résumé", 32);
CallWhat it does
wcsxfrm(key, L"résumé", 32)Builds a locale collation key for a wide string in a typical call.

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

Library declaration
size_t wcsxfrm(wchar_t *restrict dest, const wchar_t *restrict src, size_t count);

Parameters

ParameterDescription
destWritable destination with enough room for the requested result.
srcSource pointer; conversion functions update it as input is consumed.
countMaximum number of wchar_t elements to inspect, copy, append, or transform.

Return Value

Return ValueDescription
size_t countThe number of wide characters produced or required; zero can mean the output did not fit.

C Examples

Example 1 — Build a collation key

This example uses wcsxfrm() to build a collation key, then exposes the result so you can connect the call with its output.

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

int main(void) {
    setlocale(LC_COLLATE, "");
    wchar_t key[32];
    size_t n = wcsxfrm(key, L"apple", 32);
    wprintf(L"key length = %zu\n", n);
    return 0;
}

How It Works

  1. Set up this case. The program prepares the exact input needed to build a collation key.
  2. Apply the wide-string operation. wcsxfrm(key, L"résumé", 32) works in wchar_t elements and respects the ending L'\0'.
  3. Read the normal result. The displayed output shows the direct result of the call for this input.
  4. Interpret the output. key length = 5 confirms the behavior demonstrated by this example.

Example 2 — Query the required size

This example uses wcsxfrm() to query the required size, then exposes the result so you can connect the call with its output.

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

int main(void) {
    size_t needed = wcsxfrm(NULL, L"résumé", 0);
    wprintf(L"needed = %zu\n", needed);
    return 0;
}

How It Works

  1. Choose the edge case. This example changes the input so you can see how wcsxfrm() behaves for query the required size.
  2. Apply the wide-string operation. wcsxfrm(key, L"résumé", 32) works in wchar_t elements and respects the ending L'\0'.
  3. Inspect the status or pointer. The program checks what the function returned before relying on output data.
  4. Interpret the output. needed = 6 confirms the behavior demonstrated by this example.

Example 3 — Compare transformed keys

This example uses wcsxfrm() to compare transformed keys, 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 a[32], b[32];
    wcsxfrm(a, L"apple", 32); wcsxfrm(b, L"banana", 32);
    wprintf(L"ordered = %ls\n", wcscmp(a, b) < 0 ? L"yes" : L"no");
    return 0;
}

How It Works

  1. Prepare a practical variation. The data is arranged specifically to compare transformed keys.
  2. Apply the wide-string operation. wcsxfrm(key, L"résumé", 32) works in wchar_t elements and respects the ending L'\0'.
  3. Notice the safety rule. The example keeps the count, destination capacity, stream state, and terminator requirements visible.
  4. Interpret the output. ordered = yes confirms the behavior demonstrated by this example.

Frequently Asked Questions

wcsxfrm() builds a locale collation key for a wide string. It is part of the standard wide-character library.
Its parameters are dest, src, count. dest: Writable destination with enough room for the requested result. src: Source pointer; conversion functions update it as input is consumed. count: Maximum number of wchar_t elements to inspect, copy, append, or transform.
size_t count means the number of wide characters produced or required; zero can mean the output did not fit.
Include <wchar.h> for wcsxfrm(). Include only the supporting headers used by your program, such as <stdio.h> for FILE or <stdarg.h> for va_list.
Inputs must be valid null-terminated wide strings unless the API explicitly uses a count. Writable destinations need room for the result and L'\0'.

Next: C wctob() 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