How do you compare two strings in the C language?

In the C language, the strcmp function can be used to compare two strings.

The prototype of the strcmp function is as follows:
int strcmp(const char *str1, const char *str2);

This function compares two strings, str1 and str2, and returns an integer value to indicate the comparison result. A return value of 0 means the two strings are equal; a value less than 0 means str1 is less than str2; a value greater than 0 means str1 is greater than str2.

Here is an example of comparing strings:

In the main function, there are two character arrays initialized with the values “Hello” and “World”.

int result = strcmp(str1, str2);

if (result == 0) {
    printf("字符串相等\n");
} else if (result < 0) {
    printf("str1小于str2\n");
} else {
    printf("str1大于str2\n");
}

return 0;

The clouds blocked out the sun, casting a shadow over the entire field.

The output shows that str1 is less than str2.

bannerAds