C strxfrm() Function

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

Definition

In C, strxfrm() builds a transformed sort key from a C-string for the current locale. After that, a normal strcmp() on the keys follows locale order.

You pass the destination, the source, and the destination size. All three are required. The return value is the length needed for the key. Call setlocale first. Include <string.h>.

Syntax

Pass destination, source, and size. Start with this call.

C
strxfrm(dest, src, sizeof dest);
CallWhat it does
strxfrm(dest, src, sizeof dest)Writes a locale sort key for src into dest.

Include <string.h> and <locale.h>. Call setlocale(LC_COLLATE, "") before transforming.

Library declarations
C
size_t strxfrm(char *dest, const char *src, size_t n);

Parameters

ParameterDescription
destRequired. The array that receives the sort key when n is greater than 0.
srcRequired. The C-string to transform.
nRequired. How many bytes dest can hold, including the ending '\0'.

Return Value

Return ValueDescription
Length neededThe length of the transformed key, not counting the ending '\0'.

C Examples

Example 1 — Transform Into a Buffer

Write a sort key into dest and print the returned length.

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

int main(void) {
    char dest[32];
    size_t n;

    n = strxfrm(dest, "apple", sizeof dest);
    printf("key    = %s\n", dest);
    printf("length = %zu\n", n);

    return 0;
}

How It Works

  1. Provide dest and n. sizeof dest is 32 bytes including room for '\0'.
  2. Transform src. Under the C locale, the key is often just "apple".
  3. Read the return. 5 is the full key length without the terminator.
  4. Check truncation. If the return were >= 32, you would need a bigger buffer.

Example 2 — Query Size, Then Transform

Call with n = 0 to learn how large dest must be, then transform into a sized array.

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

int main(void) {
    const char *src = "hello";
    size_t need = strxfrm(NULL, src, 0);
    char dest[64];

    if (need + 1 > sizeof dest) {
        printf("buffer too small\n");
        return 1;
    }

    strxfrm(dest, src, need + 1);
    printf("need = %zu\n", need);
    printf("key  = %s\n", dest);

    return 0;
}

How It Works

  1. Ask for length. strxfrm(NULL, src, 0) returns bytes needed without '\0'.
  2. Guard the buffer. Ensure need + 1 fits in dest.
  3. Transform for real. Pass need + 1 so the terminator fits.
  4. Use the key. Ready for sorting or storage.

Example 3 — Compare Keys with strcmp

Build two keys and compare them. The sign of strcmp on keys matches strcoll on the originals (for the current locale).

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

int main(void) {
    char key1[32];
    char key2[32];
    int cmp;

    strxfrm(key1, "cat", sizeof key1);
    strxfrm(key2, "dog", sizeof key2);

    cmp = strcmp(key1, key2);
    if (cmp < 0) {
        printf("cat before dog\n");
    } else if (cmp > 0) {
        printf("dog before cat\n");
    } else {
        printf("equal\n");
    }

    return 0;
}

How It Works

  1. Transform both strings. Each key lives in its own buffer.
  2. Compare with strcmp. Fast byte compare on the keys.
  3. Read the sign. Negative means key1 sorts before key2.
  4. Why bother? When sorting a list, transform once per string, then strcmp many times.

Frequently Asked Questions

strxfrm() builds a locale sort key from a C-string so later strcmp calls follow locale order.
All three are required. dest receives the key, src is the source, and n is the dest size.
The length needed for the transformed key, not counting the ending '\0'.
Include <string.h>. Also include <locale.h> when you call setlocale.
If the return value is greater than or equal to n, the key was truncated.

More string functions

Browse the full index of C string and memory helpers.

String functions →

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