|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <string.h>
- int main ()
- {
- char a;
- char b;
-
- printf("输入a:");
- scanf("%c",&a);
- getchar();
-
- printf("输入b:");
- scanf("%c",&b);
- getchar();
-
- if (strcmp(a,b))
- {
- printf("the same!");
- }
- else
- {
- printf("not the same!");
- }
- }
复制代码
为啥会不行,我的想法是输入两个字符然后比较,但是程序会停止运行?
本帖最后由 jackz007 于 2020-2-19 15:56 编辑
a 和 b 是单个字符,用
- if (a == b) printf("the same!");
- else printf("not the same!");
复制代码
判断相等就可以了,strcmp(a , b) 这种用法在这里是错误的,strcmp() 要求两个输入参数是字符串,而不可以是单个字符。况且,判断 2 个字符串相等是这样表达的:
- if (strcmp(a , b) == 0) printf("the same!");
- else printf("not the same!");
复制代码
当然,这么说并不意味着可以在本例中使用 strcmp() 来判断,使用 strcmp() 的前提是,a 、b 必须是字符串
|
|