c - 我在学习 c 时无法说"is"

标签 c

我正在尝试学习c,我想用一个字符做一个yesno。这是我的代码:

我已经尝试过了

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

//just playing

int main()
{
    char yesno;
    printf("TEST [Y/n] ");
    scanf(" %c", &yesno);
    if(yesno == "y") {
        printf("You did yes!!");
    } else if(yesno == "n") {
        printf("You did no!");
    } else {
        printf("Not valid!!!");
    }
    return 0;
}

并且它会自动跳到else 这是为什么

更新:我不知道这是重复的。我不知道双引号和单引号之前是什么

最佳答案

这里

yesno == "y"

您正在将字符 yesno 与字符串 "y" 进行比较。应该是

yesno == 'y'  /* use single quotation instead of double quotation */

旁注始终使用最少的以下标志来编译您的代码,并且不要忽略警告,请处理这些警告。例如

gcc -Wall -Wstrict-prototypes -Wpedantic -Werror test.c

其中 Werror 将警告转换为错误并停止编译,以便您不应该跳转到输出并强制您阅读并解决编译器错误消息。

关于c - 我在学习 c 时无法说"is",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53919534/

相关文章:

c - 生成整数数组

c - 为什么转换 'out of range integer to integer' 会导致 IB,但转换 'out of range floating-point to integer' 会导致 UB?

c - 无法理解 C 程序中 "system"函数调用的行为

c++ - 在一系列 char* 缓冲区中寻找序列号?

c - Windows 上的 malloc

c - Docker 不遵循构建目录中的符号链接(symbolic link)

C 程序 - 接收段错误

在 Visual Studio 2012 Express 中将 C 代码编译为 .bin 文件

c - 线程要处理

c - 这个C程序在做什么?整数运算**