c - scanf 的问题

标签 c scanf

#include <stdio.h>

int main()
{

    char C, B;
    int x;



    printf("What comes after G\n");
    scanf("%c", &C);

    printf("What comes after O\n");
    scanf("%c", &B);

    printf("What is your age?\n");
    scanf("%d", &x);

    printf("You said %c comes after G, %c after T and you're %d years old? Right?", C, B, x);

    return 0;
}

问题 是每当您运行代码时,它都会跳过第二个问题“O 之后是什么”,然后问“您的年龄是多少?”

避免程序跳过第二个问题的唯一方法是在代码中添加一个空格

printf("What comes after O\n");
    scanf(" %c", &B);

可以看到"%c之间的空格

你能给我解释一下吗?

最佳答案

您需要吃掉空白(即新行)- 按照手册页 http://linux.die.net/man/3/scanf

关于c - scanf 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20139415/

相关文章:

c++ - 库依赖 C++ 项目

c语言段错误

c - 如何在 C 中扫描 long long long int

c - 数组分配后 scanf 挂起...0.o?

c++ - 使用 scanf() 获取数组中的字符(不以空格分隔)

c - 在 c 中表示任意大数字的最佳方法是什么?

Haskell 的 inline-C 可以将 typedef 返回到函数指针吗?

c - 空指针为什么我得到这个输出?

c - 是否可以定义 const 值并要求用户输入它?

c - 我正在尝试根据子函数的退出值从主函数进行打印