为isupper()和朋友转换为unsigned char [英] Casting to unsigned char for isupper() and friends

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

问题描述

我已经读过你应该总是将你传递的论点转换为

isupper(),isalnum()等等,以取消签名,即使他们的

签名是int是...(int)。


由于以下原因,这让我感到困惑。是...()函数可以

接受一个字符或EOF。但现在假设(很常见)

EOF ==(int)-1。然后(unsigned char)EOF将是255,这是一个有效的

字符值!因此,这个演员会破坏将EOF

传递给......()的可能性,实际上在这种情况下会产生误导性的结果。

I''ve read that you should always cast the argument you pass to
isupper(), isalnum(), etc. to unsigned char, even though their
signature is int is...(int).

This confuses me, for the following reason. The is...() functions can
either accept a character, or EOF. But now suppose (as is common) that
EOF==(int) -1. Then (unsigned char) EOF will be 255, which is a valid
character value! So this casting destroys the possibility to pass EOF
to is...(), and in fact gives misleading results in this case.

推荐答案

Fr ************ @ googlemail.com 写道:

我读过你应该总是把你传递的参数投射到

isupper( ),isalnum()等等到unsigned char,即使它们的

签名是int也是......(int)。


这让我很困惑,由于以下原因。是...()函数可以

接受一个字符或EOF。但现在假设(很常见)

EOF ==(int)-1。然后(unsigned char)EOF将是255,这是一个有效的

字符值!所以这个演员破坏了将EOF

传递给......()的可能性,实际上在这种情况下会产生误导性的结果。
I''ve read that you should always cast the argument you pass to
isupper(), isalnum(), etc. to unsigned char, even though their
signature is int is...(int).

This confuses me, for the following reason. The is...() functions can
either accept a character, or EOF. But now suppose (as is common) that
EOF==(int) -1. Then (unsigned char) EOF will be 255, which is a valid
character value! So this casting destroys the possibility to pass EOF
to is...(), and in fact gives misleading results in this case.



如果你有一个type(plain)char的值,你应该把它转换为

unsigned char,然后再将它传递给isupper() (或任何*()

函数)。例如,如果签署了普通字符,则-42

可能是有效字符;你需要将它转换为unsigned char,

屈服(假设8位字符)值214,isupper()

可以理解。


如果你有值EOF,那么你可能还没有尝试将它存储在char类型的变量中。例如,如果它是

getchar()函数的结果,那么它已经是int类型(和任何具有负值的字符

)由于signed char已经转换为

unsigned char),因此不需要强制转换。如你所说,将它转换为unsigned char

会丢失信息。


所以说你应该*总是*将参数转换为unsigned char

并不完全正确。但是将值EOF传递给

的能力是*()函数是相当模糊的,并且它不是我曾经看过的事情。对于。你是对的,EOF是规则的例外,

但是我建议在第一个

的地方避免使用EOF。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

If you have a value of type (plain) char, you should cast it to
unsigned char before passing it to isupper() (or any of the is*()
functions). For example, if plain char is signed, then -42
might be a valid character; you need to convert it to unsigned char,
yielding (assuming 8-bit characters) the value 214, which isupper()
can understand.

If you have the value EOF, then presumably you haven''t tried to store
it in a variable of type char. For example, if it''s the result of the
getchar() function, then it''s already of type int (and any characters
that have negative values as signed char are already converted to
unsigned char), so no cast is necessary. Casting it to unsigned char
would, as you say, lose information.

So saying that you should *always* cast the argument to unsigned char
isn''t quite correct. But the ability to pass the value EOF to the
is*() functions is fairly obscure, and it''s not something I''ve ever
seen a use for. You''re correct that EOF is an exception to the rule,
but I''d recommend just avoiding EOF in this context in the first
place.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


2007年3月23日16 :30:13 -0700,在comp.lang.c,
Fr ***** *******@googlemail.com 写道:
On 23 Mar 2007 16:30:13 -0700, in comp.lang.c ,
Fr************@googlemail.com wrote:

>我已经读过你应该总是把你传递的参数强制转换为
isupper(),isalnum()等等到unsigned char,即使它们的
签名是int也是......(int)。

这让我感到困惑,因为以下原因。是...()函数可以接受一个字符或EOF。但现在假设(很常见)
EOF ==(int)-1。然后(unsigned char)EOF将是255,这是一个有效的
字符值!所以这个演员破坏了将EOF传递给......()的可能性,实际上在这种情况下会产生误导性的结果。
>I''ve read that you should always cast the argument you pass to
isupper(), isalnum(), etc. to unsigned char, even though their
signature is int is...(int).

This confuses me, for the following reason. The is...() functions can
either accept a character, or EOF. But now suppose (as is common) that
EOF==(int) -1. Then (unsigned char) EOF will be 255, which is a valid
character value! So this casting destroys the possibility to pass EOF
to is...(), and in fact gives misleading results in this case.



虽然你可以将EOF传递给这些函数但它没有任何用处

这样做我能想到。我怀疑它在那里,因为getchar()

和同类可以返回它。


另一方面,超出unsigned char范围之外的任何其他值

将调用未定义的行为。因此,演员是一种安全措施,以防止意外调用UB。


-

Mark McIntyre


调试是第一次编写代码的两倍。

因此,如果你尽可能巧妙地编写代码,那么,你是>
的定义,不够聪明,不能调试它。

--Brian Kernighan

While you can pass EOF to these functions it serves no useful purpose
to do so that I can think of. I suspect its there because getchar()
and the ilk can return it.

On the other hand, any other value outside the range of unsigned char
would invoke undefined behaviour. The cast is thus a safety measure to
prevent accidental invocation of UB.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan


文章< ln * ***********@nuthaus.mib.org>,

Keith Thompson< ks *** @ mib.orgwrote:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:

>但是将值EOF传递给
的能力是*()函数是相当模糊的,并且它不是我曾经看过的用途对于。
>But the ability to pass the value EOF to the
is*() functions is fairly obscure, and it''s not something I''ve ever
seen a use for.



我想如果你有一系列的测试,比如


c = getchar();

if(isupper(c))

...;

else if(isdigit(c))

...;

else if(c ==''*'')

...;

else if(c == EOF)

...;


你可以做到这一点而不用担心测试的顺序,就像

它只有相等的测试一样。


- 理查德

-

考虑需要多达32个字符" - 1963年的X3.4。

I suppose if you have a series of tests like

c = getchar();
if(isupper(c))
...;
else if(isdigit(c))
...;
else if(c == ''*'')
...;
else if(c == EOF)
...;

you can do it without worrying about the order of the tests, just as if
it only had equality tests.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.


这篇关于为isupper()和朋友转换为unsigned char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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