scanf 的问题 [英] Problems with scanf

查看:52
本文介绍了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 之后是什么",然后询问您的年龄是多少?"

The problem is whenever you run the code it skips the second question "What comes after O" and then asks "What is your age?"

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

The only way I could avoid the program skip the 2nd question was by adding a space to the code

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

您可以看到 "%c

你能给我解释一下吗?

推荐答案

你需要吃掉空白区域(即换行) - 根据手册页 http://linux.die.net/man/3/scanf

You need to eat up the white space (i.e. new line) - as per the manual page http://linux.die.net/man/3/scanf

这篇关于scanf 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆