lol*_*fly 4 c char assignment-operator conditional-statements
我正在学习c.我有个问题.为什么我的程序不起作用?
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
char cmd;
void exec()
{
if (cmd == "e")
{
printf("%c", cmd);
// exit(0);
}
else
{
printf("Illegal Arg");
}
}
void input()
{
scanf("%c", &cmd);
exec();
}
int main()
{
input();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我插入一个"e",但它说非法arg.
cmd不等于"e".为什么?我用scanf将cmd设置为"e".
Cas*_*yer 25
首先,在C单引号是char文字,双引号是字符串文字.因此,'C'和"C"不是一回事.
要进行字符串比较,请使用strcmp.
const char* str = "abc";
if (strcmp ("abc", str) == 0) {
printf("strings match\n");
}
Run Code Online (Sandbox Code Playgroud)
要进行字符比较,请使用相等运算符.
char c = 'a';
if ('a' == c) {
printf("characters match\n");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48587 次 |
| 最近记录: |