为什么一定要用来保存的getchar的返回值的变量声明为int? [英] Why must the variable used to hold getchar's return value be declared as int?

查看:255
本文介绍了为什么一定要用来保存的getchar的返回值的变量声明为int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C语言编程初学者,最近我研究过的的getchar 功能, getchar函数将接受来自控制台或文件中的字符,显示立即打字时,我们需要以preSS输入程序键。

I am beginner in C programming language, recently I have studied about getchar function, getchar function will accept a character from the console or from a file, displays immediately while typing and we need to press Enter key for proceeding.

它返回的 unsigned char型他们read.If档案结尾或遇到错误的getchar()函数返回EOF。

It returns the unsigned char that they read.If end-of-file or an error is encountered getchar() functions return EOF.

我的问题是,当它返回的 unsigned char型,然后为什么它返回的值存储在 INT 变量?

My question is that, When it returns unsigned char, then why its returned value is stored in int variable?

请帮我。

推荐答案

precisely因为EOF价值的。由于在文件中的字符可以是任何可能的字符值,包括空字符C字符串用于终止的getchar()必须使用较大的整型来添加EOF价值。

Precisely because of that EOF-value. Because a char in a file may be any possible char value, including the null character that C-strings use for termination, getchar() must use a larger integer type to add an EOF-value.

它只是恰好将int用于这一目的,但它可以使用任何类型的具有至少9位

It simply happens to use int for that purpose, but it could use any type with at least 9 bit.

这篇关于为什么一定要用来保存的getchar的返回值的变量声明为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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