This question already has answers here:
How do I properly compare strings?
                                
                                    (8个答案)
                                
                        
                        
                            string comparison inside if condition malfunctioning [duplicate]
                                
                                    (2个答案)
                                
                        
                                2年前关闭。
            
                    
#include<stdio.h>

int main(){

char name[20];

scanf(" %s", name);

if (name == 'James'){

    printf("Welcome James");
}else{

    printf("You are not James");
}

}


希望你有我的主意。我知道这行不通。但是有什么办法可以做这样的事情吗?

最佳答案

采用

if (strcmp(name, "James") == 0)


代替

if (name == 'James')


strcmp的手册页:

返回值


  strcmp()strncmp()函数返回小于的整数,
  如果s1(或其前n个字节)为,则等于或大于零
  发现分别小于,匹配或大于s2。

关于c - 在C程序中,如何检查输入字符串是否为特定名称? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47150853/

10-11 23:29