How to compare strings in the C language.

In C language, the strcmp function can be used to compare whether two strings are the same. The strcmp function compares the two strings and returns an integer value that indicates the comparison result.

If the return value is 0, it means that the two strings are the same; if the return value is less than 0, it means the first string is less than the second string; if the return value is greater than 0, it means the first string is greater than the second string.

Here is an example:

#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Hello";
    char str2[] = "Hello";
    
    int result = strcmp(str1, str2);
    
    if (result == 0) {
        printf("两个字符串相同\n");
    } else {
        printf("两个字符串不同\n");
    }
    
    return 0;
}

The output result is “the two strings are the same”.

bannerAds