char和int的 [英] char's and int's

查看:68
本文介绍了char和int的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在标准库中看到了很多处理

字符的函数,但是很多函数返回/接受int类型的参数(

通常是表示

字符集中字符代码的整数。我知道在没有问题的情况下C会自动在int和

char之间进行转换(或者可能是烧焦/未签名的
问题导致问题吗?),并且字符常量实际上已经存在

类型int,但是,标准

库中的某些函数返回类型int(或者采用int类型的参数)的原因是什么?

应该是处理字符而不是数字?


谢谢,

塞巴斯蒂安

I''ve seen a lot of functions in the standard library that deal with
characters, but a lot them return/take parameters of type int (which
is usually the integer that represents the character code in the
character set). I know that C automatically converts between int and
char in those cases without problems (or could the singed/unsigned
issue cause problems?), and that character constants have actually
type int, but, what is the reason for some functions in the standard
library to return type int (or take parameters of type int) when one
is supposed to be dealing with characters instead of numbers?

Thanks,
Sebastian

推荐答案

7月4日12:49 pm,s0s ... @ gmail.com写道:
On Jul 4, 12:49 pm, s0s...@gmail.com wrote:

我'已经看到标准库中有很多函数处理

字符,但很多它们返回/接受int类型的参数(

通常是整数表示

字符集中的字符代码。我知道在没有问题的情况下C会自动在int和

char之间进行转换(或者可能是烧焦/未签名的
问题导致问题吗?),并且字符常量实际上已经存在

类型int,但是,标准

库中的某些函数返回类型int(或者采用int类型的参数)的原因是什么?

应该是处理字符而不是数字?


谢谢,

Sebastian
I''ve seen a lot of functions in the standard library that deal with
characters, but a lot them return/take parameters of type int (which
is usually the integer that represents the character code in the
character set). I know that C automatically converts between int and
char in those cases without problems (or could the singed/unsigned
issue cause problems?), and that character constants have actually
type int, but, what is the reason for some functions in the standard
library to return type int (or take parameters of type int) when one
is supposed to be dealing with characters instead of numbers?

Thanks,
Sebastian



Cosider fgetc()。它实际上返回一个无符号整数强制转换为

int。需要转换为int,因为EOF被定义为负数

int(通常,但不一定是-1)。所以,除了EOF之外,我们

得到一个unsigned int。

Cosider fgetc(). It actually returns an unsigned integer cast to an
int. Casting to the int is required because EOF is defined as negative
int(generally, but not necessarily, -1). So, other than the EOF, we
get an unsigned int.


rahul说:


< snip>
rahul said:

<snip>

Cosider fgetc()。它实际上返回一个无符号整数强制转换为

int。
Cosider fgetc(). It actually returns an unsigned integer cast to an
int.



因为字符是整数,所以你是正确的。

Since chars are integers, you are correct.


因为EOF是,所以需要转换为int。定义为否定

int(通常,但不一定是-1)。所以,除了EOF,我们

得到一个unsigned int。
Casting to the int is required because EOF is defined as negative
int(generally, but not necessarily, -1). So, other than the EOF, we
get an unsigned int.



不,除了EOF之外,我们得到一个int,它是将

无符号字符转换为int的结果。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http: //万维网。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

No, other than the EOF we get an int that is the result of a conversion of
an unsigned char to an int.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


7月4日,09:59,Richard Heathfield< r ... @ see.sig.invalidwrote:
On 4 Jul, 09:59, Richard Heathfield <r...@see.sig.invalidwrote:

rahul说:


< snip>
rahul said:

<snip>

Cosider fgetc()。它实际上返回一个无符号整数强制转换为

int。
Cosider fgetc(). It actually returns an unsigned integer cast to an
int.



因为字符是整数,所以你是正确的。


Since chars are integers, you are correct.


因为EOF是,所以需要转换为int定义为否定

int(通常,但不一定是-1)。所以,除了EOF,我们

得到一个unsigned int。
Casting to the int is required because EOF is defined as negative
int(generally, but not necessarily, -1). So, other than the EOF, we
get an unsigned int.



不,除了EOF之外,我们得到一个int,它是将无符号字符转换为int的结果。


No, other than the EOF we get an int that is the result of a conversion of
an unsigned char to an int.



不是指将char转换为int ?

Don''t you mean "a conversion of a char to an int" ?


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

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