`strcmp` is a function in the C programming language that is used to compare two strings. It returns an integer value that indicates the lexicographic relationship between the two strings.

The function takes two parameters, `str1` and `str2`, which are the strings to be compared. It compares the characters of the strings one by one until it finds a difference or reaches the end of either string.

The return value of `strcmp` is as follows:

  • If `str1` is less than `str2`, it returns a negative integer.
  • If `str1` is greater than `str2`, it returns a positive integer.
  • If both strings are equal, it returns 0.

Here is an example usage of `strcmp`:

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

int main() {
    char str1[] = "Hello";
    char str2[] = "World";
    
    int result = strcmp(str1, str2);
    
    if (result < 0) {
        printf("%s is less than %s\n", str1, str2);
    } else if (result > 0) {
        printf("%s is greater than %s\n", str1, str2);
    } else {
        printf("%s is equal to %s\n", str1, str2);
    }
    
    return 0;
}

Output:

Hello is less than World

原文地址: https://www.cveoy.top/t/topic/p83K 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录