Reference:C Functions and Keywords/ANSI-ISO Standard C/strcmp()

From CoderGuide

Jump to: navigation, search

Language Cross Reference

  Java Perl PHP Python

string comparison functions

#include <string.h>
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);

strcmp() and strncmp() function similarly: They compare s1 to s2 and return a value of zero if equal (which would be interpreted as a logical false on it's own), or greater than or less than zero if the strings do not match (which would be interpreted as a logical true). strncmp() will only compare the first size_t n' bytes to prevent buffer overruns.

The fact that strcmp()' returns non-zero if the strings do not match is useful during sorting. If the number is less than zero, then the string s1 has less of a sorting value than s2, that is, the string "Alpha" is less than the string "Beta". This is important to keep in mind when using string comparison functions because:

        printf("I won't be displayed, because the difference between the two strings is zero");
        printf("I will be displayed because ! negates the zero returned by strcmp");

wcscmp() is similar to strcmp(), except it uses wchar_t instead of char arrays.

See Also

strcat(), strncat(), strcpy(), strncpy(), strcasecmp(), wcscmp()

Personal tools