如何检查两个字符串在C中是否相同?

时间:2018-08-17 22:09:28

标签: c string c-strings

我刚刚开始学习 C ,并且我试图习惯语法,因此,当我尝试运行该程序并输入值时,它什么也不会返回。我不知道我做错了什么。有人可以帮我吗?

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

int main()
{
    char pass[10]; //max length of the string;
    do{
        printf("enter your password: \n");
        scanf("%s",pass);
    }while(strcmp(pass,'*') != 0); //checks if strings are equal

    printf("%s",pass);


}

1 个答案:

答案 0 :(得分:4)

在C中,单引号用于字符常量。因此,'*'不是包含字符*的字符串,而是字符*

要指定字符串,请使用双引号。

 }while(strcmp(pass,"*") != 0);