C++ strxfrm() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cstring>

Definition

In C++, strxfrm() transforms a C-string into another byte sequence that can be compared with strcmp to get locale-aware ordering.

Include <cstring>. Related: strcoll.

This guide shows the C library helper in a C++ .cpp program with std::cout. Prefer std::string for new text code; keep these APIs for C interop and interviews.

Syntax

Pass destination, source, and destination capacity.

C++
std::size_t n = std::strxfrm(dest, src, dest_size);
CallWhat it does
std::strxfrm(dest, src, n)Write transformed form into dest (at most n bytes).
std::strxfrm(nullptr, src, 0)Return length needed (not including the idea of writing).

If n is 0 and dest is null, strxfrm returns the length required for the transformed string.

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

Parameters

ParameterDescription
destOutput buffer (or null when querying length).
srcSource C-string.
nCapacity of dest.

Return Value

Return ValueDescription
size_tLength of the transformed string (not counting terminator in the usual description—check your libc docs).

C++ Examples

Example 1 — Query Length

Ask how long the transform is.

C++
#include <cstring>
#include <iostream>

int main() {
    const char* src = "ab";
    std::size_t need = std::strxfrm(nullptr, src, 0);
    std::cout << need << '\\n';
    return 0;
}

How It Works

  1. Passing null dest with n=0 requests the size.
  2. For the C locale, transform of short ASCII often matches length.
  3. Exact value can vary by locale.

Example 2 — Write Transform

Transform into a buffer then strcmp.

C++
#include <cstring>
#include <iostream>

int main() {
    char a[8];
    char b[8];
    std::strxfrm(a, "ab", sizeof a);
    std::strxfrm(b, "ab", sizeof b);
    std::cout << std::strcmp(a, b) << '\\n';
    return 0;
}

How It Works

  1. Both sources are equal.
  2. Transformed forms compare equal with strcmp.
  3. Result 0.

Example 3 — Same Locale Idea

Equal ASCII strings compare equal after transform.

C++
#include <cstring>
#include <iostream>

int main() {
    char x[8];
    char y[8];
    std::strxfrm(x, "a", sizeof x);
    std::strxfrm(y, "b", sizeof y);
    std::cout << (std::strcmp(x, y) < 0 ? "a<b" : "a>=b") << '\\n';
    return 0;
}

How It Works

  1. Transforms are produced for a and b.
  2. strcmp on transforms orders them.
  3. In the C locale, a comes before b.

Frequently Asked Questions

Transforms a string for locale-sensitive strcmp comparisons.
strcoll compares with locale rules directly.
<cstring>.
Rarely—only when sorting text under a locale.
Call strxfrm(nullptr, src, 0) then allocate.

Back to the function index

See every string-related function guide in one place.

Function index →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

11 people found this page helpful