C语言 在 C 中比较用户输入的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3911653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comparing user-inputted characters in C
提问by Joe Scho
The following code snippets are from a C program.
以下代码片段来自 C 程序。
The user enters Y or N.
用户输入 Y 或 N。
char *answer = 'char answer;
scanf(" %c",&answer);
';
scanf (" %c", answer);
if (*answer == ('Y' || 'y'))
// do work
I can't figure out why this ifstatement doesn't evaluate to true.
我不明白为什么这个if陈述不评估为真。
I checked for the y or n input with a printfand it is there, so I know I'm getting the user input. Also when I replace the the condition of the if statement with 1 (making it true), it evaluates properly.
我用 a 检查了 y 或 n 输入,printf它就在那里,所以我知道我正在获取用户输入。此外,当我将 if 语句的条件替换为 1(使其为真)时,它会正确评估。
回答by codaddict
I see two problems:
我看到两个问题:
The pointer answeris a nullpointer and you are trying to dereference it in scanf, this leads to undefined behavior.
指针answer是一个null指针,您试图在 中取消引用它scanf,这会导致未定义的行为。
You don't need a charpointer here. You can just use a charvariable as:
char这里不需要指针。您可以将char变量用作:
if( answer == 'y' || answer == 'Y') {
// user entered y or Y.
}
Next to see if the read character is 'y'or 'Y'you should do:
接下来看看读取的字符是否是'y'或者'Y'你应该这样做:
char var;
char *answer = &var; // make answer point to char variable var.
scanf (" %c", answer);
if( *answer == 'y' || *answer == 'Y') {
If you reallyneed to use a char pointer you can do something like:
如果您确实需要使用字符指针,您可以执行以下操作:
char answer;
scanf(" %c", &answer);
回答by Mark Elliot
answershouldn't be a pointer, the intent is obviously to hold a character. scanftakes the address of this character, so it should be called as
answer不应该是一个指针,意图显然是持有一个字符。 scanf取这个字符的地址,所以它应该被称为
if (answer == 'Y' || answer == 'y')
Next, your "or" statement is formed incorrectly.
接下来,您的“或”语句形成不正确。
switch (*answer) {
case 'Y':
case 'y':
/* Code for Y */
break;
default:
/* Code for anything else */
}
What you wrote originally asks to compare answerwith the result of 'Y' || 'y', which I'm guessing isn't quite what you wanted to do.
您最初写的内容要求answer与 的结果进行比较'Y' || 'y',我猜这不是您想要做的。
回答by hobbs
Because comparison doesn't work that way. 'Y' || 'y'is a logical-or operator; it returns 1(true) if either of its arguments is true. Since 'Y'and 'y'are both true, you're comparing *answerwith 1.
因为比较不是这样。'Y' || 'y'是逻辑或运算符;1如果其中一个参数为真,则返回(true)。由于'Y'和'y'都为真,因此您正在*answer与 1进行比较。
What you want is if(*answer == 'Y' || *answer == 'y')or perhaps:
你想要的是if(*answer == 'Y' || *answer == 'y')或者也许是:
if (answer == ('Y' || 'y'))
回答by paxdiablo
For a start, your answervariable should be of type char, not char*.
首先,您的answer变量应该是 类型char,而不是char*。
As for the ifstatement:
至于if声明:
if ((answer == 'Y') || (answer == 'y'))
This is first evaluating 'Y' || 'y'which, in Boolean logic (and for ASCII) is true since both of them are "true" (non-zero). In other words, you'd only get the ifstatement to fire if you'd somehow entered CTRLA(again, for ASCII, and where a true values equates to 1)*a.
这是首先评估'Y' || 'y'哪个在布尔逻辑(以及 ASCII)中为真,因为它们都是“真”(非零)。换句话说,if如果您以某种方式输入CTRLA(同样,对于 ASCII,并且真值等于 1)*a,您只会触发该语句。
You coulduse the more correct:
你可以使用更正确的:
if (toupper(answer) == 'Y')
but you really should be using:
但你真的应该使用:
##代码##since that's the more portable way to achieve the same end.
因为这是实现相同目的的更便携的方式。
*aYou may be wondering why I'm putting in all sorts of conditionals for my statements. While the vast majority of C implementations use ASCII and certain known values, it's not necessarily mandated by the ISO standards. I know for a fact that at least one compiler still uses EBCDIC so I don't like making unwarranted assumptions.
*a您可能想知道为什么我要为我的语句添加各种条件。虽然绝大多数 C 实现使用 ASCII 和某些已知值,但 ISO 标准不一定强制要求。我知道至少有一个编译器仍在使用 EBCDIC,所以我不喜欢做出毫无根据的假设。

