温馨提示×

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

小亿
90
2024-04-01 09:43:58
栏目: 编程语言

在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"。

0