小编mhk*_*mhk的帖子

strcmp 和双相等关系运算符在 C 中不起作用

在 C 中使用以下程序,字符串比较不起作用:

#include<stdio.h>
#include<string.h>
int main(){
    char ch[]="ABC";
    printf("%d \n", strlen(ch));    // output 3
    printf("%d \n", strlen("ABC")); // output 3
    if(strcmp("ABC",ch)){
        printf("\n Matched");
    }else{
        printf("\n Not matched"); // this will be output
    }

    if("ABC" == ch){
        printf("\n Matched");
    }else{
        printf("\n Not matched"); // this will be output
    }
}//ends main
Run Code Online (Sandbox Code Playgroud)

输出是:

3

3

 Not matched

 Not matched
Run Code Online (Sandbox Code Playgroud)

为什么字符串不匹配?

c string string-comparison

-1
推荐指数
1
解决办法
51
查看次数

标签 统计

c ×1

string ×1

string-comparison ×1