strcmp在c语言中怎么用
          发布时间:2023-11-20 02:39:03
        
        
          发布人:xqq
        
        
       
      strcmp() 函数是 C 语言标准库中的一个字符串操作函数,用于比较两个字符串的大小关系。它的声明在头文件 

int strcmp(const char *s1, const char *s2);
该函数接受两个参数 s1 和 s2,分别表示要比较的两个字符串。
函数返回值为整型,它表示比较结果的大小关系,具体解释如下:
如果字符串 s1 小于 s2,则返回一个小于 0 的值。
如果字符串 s1 等于 s2,则返回 0。
如果字符串 s1 大于 s2,则返回一个大于 0 的值。
以下是一个使用 strcmp() 函数的示例程序:
#include 
#include 
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 equal to %s\n", str1, str2);
    } else {
        printf("%s is greater than %s\n", str1, str2);
    }
    
    return 0;
}
  在上面的示例程序中,我们通过 strcmp() 函数比较了两个字符串 str1 和 str2,并根据比较结果输出了相应的信息。根据示例程序的运行结果,我们可以看到字符串 "hello" 小于字符串 "world",因此输出了 "hello is less than world" 的信息。


 
             
             
             
             
             
             
            