How to determine a string in the C language?

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

The strcmp function takes two strings as parameters and returns an integer value.

  1. If the two strings are equal, the return value is 0.
  2. If the first string is less than the second string, the return value will be less than 0.
  3. If the first string is greater than the second string, the return value will be greater than 0.

Here is an example:

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

int main() {
    char str1[] = "Hello";
    char str2[] = "World";

    int result = strcmp(str1, str2);

    if (result == 0) {
        printf("两个字符串相等\n");
    } else if (result < 0) {
        printf("第一个字符串小于第二个字符串\n");
    } else {
        printf("第一个字符串大于第二个字符串\n");
    }

    return 0;
}

The output shows that the first string is less than the second string.

广告
Closing in 10 seconds
bannerAds