Linux 拨号vps windows公众号手机端

c语言如何判断字符串是否相等

lewis 7年前 (2018-03-28) 阅读数 11 #程序编程
文章标签 c语言

在C语言中,可以使用strcmp()函数来判断两个字符串是否相等。strcmp()函数会比较两个字符串的内容,如果相等则返回0,如果不相等则返回非0的值。例如:

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

int main() {
    char str1[] = "hello";
    char str2[] = "hello";
    
    if (strcmp(str1, str2) == 0) {
        printf("The strings are equal\n");
    } else {
        printf("The strings are not equal\n");
    }
    
    return 0;
}

上面的代码会输出"The strings are equal",因为str1和str2的内容是相等的。如果str2的内容是"world",则会输出"The strings are not equal"。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门