C Standard Libraries C++

strncmp()

Declaration

int strncmp(const char* kcpString1, const char* kcpString2, size_t qCount);

Description

This function compares the first "qCount" characters of "kcpString1" and "kcpString2" to determine which one is less/greater than the other. The function returns a value less than 0, equal to zero, or greater than zero, depending on whether the first "qCount" characters of the first string are less than, equal to, or greater than the first "qCount" characters of the second string, respectively.

Example

#include <cstdio>
#include <cstring>

int main()
{
    char caBuffer1[] = "XoaX.net";
    char caBuffer2[] = "XoaXDotNet";
    int iComp = strncmp(caBuffer1, caBuffer2, 4);

    printf("Comparison value for four chars: %i\n", iComp);
    // Print out a message based on the comparison value.
    if (iComp < 0) {
        printf("Buffer1 < Buffer2\n");
    } else if (iComp > 0) {
        printf("Buffer1 > Buffer2\n");
    } else {
        printf("Buffer1 == Buffer2\n");
    }
    return 0;
}

Output

strncmp() Output
 

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