C Standard Libraries C++

strcmp()

Declaration

int strcmp(const char* kcpStr1, const char* kcpStr2);

Description

This function compares the two null-terminated strings "kcpStr1" and "kcpStr2" on a char by char basis 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 string is less than, equal to, or greater than the second string, respectively.

Example

#include <cstdio>
#include <cstring>

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

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

Output

strcmp() Output
 

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