C Standard Libraries C++

wcsncmp()

Declaration

int wcsncmp(const wchar_t* kqpString1,
            const wchar_t* kqpString2,
            size_t uiCompSize);

Description

This function compares the first "uiCompSize" wide-characters of each of the strings "kqpString1" and "kqpString2" and returns an integer that is less than 0 if "kqpString1" is less than "kqpString2," 0 if "kqpString1" is equal to "kqpString2," and less than 0 if "kqpString1" is greater than "kqpString2."

Example

#include <cwchar>

int main()
{
    wchar_t waString1[] = L"XoaXAlgorithms";
    wchar_t waString2[] = L"XoaXArchitecture";
    size_t uiCompSize = 5;

    // Compare the first 5 characters (should be the same)
    wprintf(L"The first %u characters of \"%ws\" vs \"%ws\"\n",
        uiCompSize, waString1, waString2);
    int iCompValue = wcsncmp(waString1, waString2, uiCompSize);
    wprintf(L"Comparison value = %d\n", iCompValue);

    // Increase the number of characters to compare
    uiCompSize = 6;
    wprintf(L"The first %u characters of \"%ws\" vs \"%ws\"\n",
        uiCompSize, waString1, waString2);
    iCompValue = wcsncmp(waString1, waString2, uiCompSize);
    wprintf(L"Comparison value = %d\n", iCompValue);

    // Reverse the order of comparison
    wprintf(L"The first %u characters of \"%ws\" vs \"%ws\"\n",
        uiCompSize, waString2, waString1);
    iCompValue = wcsncmp(waString2, waString1, uiCompSize);
    wprintf(L"Comparison value = %d\n", iCompValue);

    // Keep the window open until "Enter" is pressed
    getwchar();
    return 0;
}

Output

wcsncmp() Output
 

© 2007–2022 XoaX.net LLC. All rights reserved.