Home C String Functions strxfrm() C strxfrm() Function 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.
strxfrm(dest, src, sizeof dest); Call What 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 size_t strxfrm(char *dest, const char *src, size_t n); Parameters Parameter Description dest Required. The array that receives the sort key when n is greater than 0. src Required. The C-string to transform. n Required. How many bytes dest can hold, including the ending '\0'.
Return Value Return Value Description Length needed The length of the transformed key, not counting the ending '\0'.
C Examples 1. Basic transform 2. Query size 3. Compare keys Example 1 — Transform Into a Buffer Write a sort key into dest and print the returned length.
#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 Provide dest and n. sizeof dest is 32 bytes including room for '\0'.Transform src. Under the C locale, the key is often just "apple".Read the return. 5 is the full key length without the terminator.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.
#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 Ask for length. strxfrm(NULL, src, 0) returns bytes needed without '\0'.Guard the buffer. Ensure need + 1 fits in dest.Transform for real. Pass need + 1 so the terminator fits.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).
#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 Transform both strings. Each key lives in its own buffer.Compare with strcmp. Fast byte compare on the keys.Read the sign. Negative means key1 sorts before key2.Why bother? When sorting a list, transform once per string, then strcmp many times.Frequently Asked Questions What does strxfrm() do in C? strxfrm() builds a locale sort key from a C-string so later strcmp calls follow locale order.
What are the parameters of strxfrm()? All three are required. dest receives the key, src is the source, and n is the dest size.
What does strxfrm() return? The length needed for the transformed key, not counting the ending '\0'.
Which header file does strxfrm() need? Include <string.h>. Also include <locale.h> when you call setlocale.
How do I know if dest was too small? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion