如果getchar读取“enter”,那么getchar会返回什么? [英] What does getchar return if it reads the "enter"...?

查看:242
本文介绍了如果getchar读取“enter”,那么getchar会返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您输入一个字符并按Enter键时,它会输出两个值,因此事实证明它确实读取了输入字符。来自输入缓冲区。 getchar在读取时实际返回了什么?此外,我无法理解为什么fflush(stdin)在输入类似(bytes)的字符串然后按回车键时对输出没有任何影响。

When you type one character and press enter, it prints two values, so it turns out that it does read "enter" from the input buffer. What does getchar actually return on reading enter? Moreover I can''t understand why fflush(stdin) doesn''t have any effect on the output when you type a string like("bytes")and then press enter.

展开 | 选择 | Wrap | 行号

推荐答案

ENTER键是有效的ASCII码,由getchar()读取。尝试下面的代码片段,你会看到它的ASCII值
the ENTER key is a valid ASCII code and is read by getchar(). Try the following fragment of code and you will see its ASCII value
展开 | 选择 | Wrap | 行号


getchar返回int,EOF是一个int常量。在将它与EOF进行比较之前,不应显式或隐式地将getchar的返回值强制转换为char(例如,将其赋值给char类型的变量)。

阅读本文
getchar returns int and EOF is an int constant. You should not cast the return value of getchar to char, either explicitly or implicitly (by for example assigning it to a variable of type char) before comparing it to EOF.

read this


@banfa我认为没有在转换getchar的返回值时出现问题,首先getchar返回unsigned int,而不仅仅是任何int(是的,如果文件结束的话,它返回负值)。如果字符到整数类型转换,则有更多从来没有问题...你总是可以将一个字符转换为整数和整数到一个字符......
@banfa i dont think there is any problem in casting the return value of getchar,,,first of all getchar returns unsigned int,not just any int(yeah it returns negative value in case of end of file..)moreove in case of character to integrs type conversion,there is never of problem ...u can always cast a character to integer and integer to a character.....


这篇关于如果getchar读取“enter”,那么getchar会返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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