我想的getchar两次,但我不能 [英] i want to getchar twice but i cant

查看:173
本文介绍了我想的getchar两次,但我不能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main()
{
    int r, c;
    r = getchar();
    c = getchar();
    putchar(r);
    putchar(c);
    printf("\n");
    return(0);
}

读取后 - [R ,方案产出研究和退出。我希望它要求 C 和投入,但怎么来它没有这样做呢?

After it reads in r, the program outputs r and quits. I want it to ask for c and input it but how come it doesnt do that?

推荐答案

您在同一行中输入字符,或两行?

Are you entering the characters on the same line, or on 2 lines?

的getchar()会等到你preSS进入,然后开始分析的字符。如果您已经在2个不同的行中输入两个字符,它会读取第一个字符的然后 \\ n 字符

getchar() will wait until you press enter, and then start parsing the characters. If you have entered 2 characters on 2 different lines, it will read the first character and then the \n character.

我的意思是,下面的输入:

What I mean is, the following input:

a
b

等同于一\\ NB

的getchar()将抓住 \\ n 而不是,并打印 A \\ n \\ n

getchar() will grab the \n instead of b, and print a\n\n.

您想要输入两个字符,只有按下回车键。

You want to type both characters, and only then hit enter.

这篇关于我想的getchar两次,但我不能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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