为*()和*()函数转换为unsigned char [英] casting to unsigned char for is*() and to*() functions

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

问题描述

在使用< ctype.h>时,我一直在阅读有关将值转换为unsigned

char的做法。功能。例如,


c = toupper((unsigned char)c);


现在我明白标准是关于< ctype的.h>

函数:


"标题< ctype.h>声明了几个对分类

和映射字符有用的函数.166)在所有情况下,参数都是一个int,

的值应该可以表示为unsigned char或者

等于宏EOF的值。如果参数有任何其他值,

行为未定义。


我很难制定我的问题 - 基本上就像是

这个虽然 - 有些人说投了无符号字符(如上面的

例子),而我看到有人争辩说要投票给

unsigned char是不必要的,如果它已经完成,那么重新回到

int是必要的,因为像toupper()这样的函数需要一个int,例如


toupper((int)((unsigned char)c));


那么正确的做法是什么?转为无符号字符?转换为

unsigned char并返回int?

解决方案

----- BEGIN PGP签名消息-----

哈希:SHA1

mr **********@hotmail.com 写道:

我一直在阅读有关在使用<时使用无符号
字符串转换值的做法。文件ctype.h>功能。例如,

c = toupper((unsigned char)c);

现在我明白标准说的是关于< ctype.h>
功能:

"标题< ctype.h>声明了几个对分类和映射字符有用的函数.166)在所有情况下,参数都是一个int,其值应该可以表示为unsigned char或者
等于宏观EOF。如果参数有任何其他值,那么行为是未定义的。

我很难制定我的问题 - 基本上它就像这样 - 但是有些人说转换为unsigned char(如上面的
示例),而我看到有人认为转换为unsigned char是不必要的,如果完成了,那么重新转回
int是必要的,因为像toupper()这样的函数需要一个int,例如,

toupper((int)((unsigned char)c));

那么什么是正确的事情吗?转为无符号字符?转换为
unsigned char并返回int?




IIRC,C标准表示执行角色的所有成员

可以表示为正值。


因为toupper()的使用仅在

执行字符集的范围内有意义,而不是任意字符这个范围之外的值是值得安全的,可以说toupper()仅适用于正值char />
值或EOF(这是一个特定的,通常是负数的char)价值)。


将参数转换为无符号字符

- - 可能会更改参数值的解释,如果它是

(负值)EOF

- - 对执行字符集的正确成员没有影响


将此转换回int,同时正确地将

参数的类型更正为int,否则(据我所知)没有其他效果。

伤害已经由施法者对无符号字符进行了修复,并且不能通过重铸为int来修正



最后,toupper()最初是为了取一个int(如在fgetc()的

返回值,那就是你应该给它的东西。如果你是

使用char数据项,首先将它们转换为int,否则

不要施放。即

#include< ctype.h>

#include< stdio.h>


{

char datum =''c'',uc_datum;

int file_datum,uc_file_datum;


file_datum = fgetc(stdin);

uc_file_datum = toupper(file_datum);


uc_datum = toupper((int)datum);

}


- -


Lew Pitcher,IT专家,企业数据系统

企业技术解决方案,道明银行金融集团


(此处表达的意见是我自己的,不是我的意见雇主') >
----- BEGIN PGP SIGNATURE -----

版本:GnuPG v1.2.4(MingW32)

iD8DBQFCwrO + agVFX4UWr64RAn4DAKCnWQEAHo7kXd8xv3DFlJ FIYDH7BQCg5W9M

REN07taxd5C5T4SJMM8JaSk =

= HH + V

----- END PGP SIGNATURE -----




mr ******** **@hotmail.com 写道:

我一直在阅读使用< ctype.h>时将值转换为无符号
char的做法。功能。例如,

c = toupper((unsigned char)c);

现在我明白标准说的是关于< ctype.h>
功能:

"标题< ctype.h>声明了几个对分类和映射字符有用的函数.166)在所有情况下,参数都是一个int,其值应该可以表示为unsigned char或者
等于宏观EOF。如果参数有任何其他值,那么行为是未定义的。

我很难制定我的问题 - 基本上它就像这样 - 但是有些人说转换为unsigned char(如上面的
示例),而我看到有人认为转换为unsigned char是不必要的,如果完成了,那么重新转回
int是必要的,因为像toupper()这样的函数需要一个int,例如,

toupper((int)((unsigned char)c));

那么什么是正确的事情吗?转为无符号字符?转换为
unsigned char并返回int?




如果`c''是一个普通的'char'',则将其强制转换为`unsigned char'' 。

对int的进一步演员是无害的,但是没有必要:因为

< ctype.h>提供了一个原型,说toupper()接受一个

`int''参数,编译器无论如何都会进行转换。


你需要演员的原因是直接转换

从普通的`char''到'int''可能不会产生toupper()

需要的东西。具体来说,如果`char''是带符号的类型且'c''具有负值b / b,则直接转换将产生负数

`int''。如果这个负int碰巧等于EOF toupper()

将只返回EOF,并且这可能不是

大写等价于`c ''。如果否定的'int''是除了EOF以外的其他东西,所有的赌注都会被取消,而且你处于未定义行为的危险境界。


如果`c''是从getc()获得的`int'',

只是传递它而不进行强制转换。 getc()及其同类

已经返回EOF或非负`unsigned char''
值,这就是toupper()等。需要。


-
Er ****** ***@sun.com


mr ********** @ hotmail.com 写道:


我一直在阅读有关将值转换为无符号的做法使用< ctype.h>时使用char功能。例如,

c = toupper((unsigned char)c);

现在我明白标准说的是关于< ctype.h>
函数:

"标题< ctype.h>
声明了几个对分类和映射字符有用的函数.166)在所有情况下,参数都是一个int,
其值应表示为无符号字符或
等于宏EOF的值。如果参数有任何其他值,那么行为是未定义的。

我很难制定我的问题 - 基本上它就像这样 - 但是有些人说转换为unsigned char(如上面的
示例),而我看到有人认为转换为unsigned char是不必要的,如果完成了,那么重新转回
int是必要的,因为像toupper()这样的函数需要一个int,例如,

toupper((int)((unsigned char)c));

那么什么是正确的事情吗?转为无符号字符?转换为
unsigned char并返回int?




由于toupper未定义为值

无法表示为unsigned char ,

然后如果对无符号字符的强制转换将改变该值,

然后这样做,如果没有,那么就不要打扰。

fputc,描述所有文件输出,

在输出之前将它的int参数转换为unsigned char。


所以,如果你有一个负整数值,如:

#define NEG_a('''' - 1 - (无符号字符)-1)

你知道吗

putchar(NEG_a);

将输出''a''字符。


要使该负数与toupper一起使用:

putchar(toupper((unsigned char)NEG_a));


-

pete


I have been reading about the practise of casting values to unsigned
char while using the <ctype.h> functions. For example,

c = toupper ((unsigned char) c);

Now I understand that the standard says this about the <ctype.h>
functions:

"The header <ctype.h> declares several functions useful for classifying
and mapping characters.166) In all cases the argument is an int, the
value of which shall be representable as an unsigned char or shall
equal the value of the macro EOF. If the argument has any other value,
the behavior is undefined."

I am having a hard time formulating my question - basically its like
this though - Some people say cast to unsigned char (as in the above
example), whereas I have seen some people argue that casting to
unsigned char is unecessary, and if it is done, then a recast back to
int is necessary, because functions like toupper() expect an int, eg,

toupper( (int)((unsigned char) c) );

So what is the right thing to do? Cast to unsigned char? Cast to
unsigned char and back to int?

解决方案

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

mr**********@hotmail.com wrote:

I have been reading about the practise of casting values to unsigned
char while using the <ctype.h> functions. For example,

c = toupper ((unsigned char) c);

Now I understand that the standard says this about the <ctype.h>
functions:

"The header <ctype.h> declares several functions useful for classifying
and mapping characters.166) In all cases the argument is an int, the
value of which shall be representable as an unsigned char or shall
equal the value of the macro EOF. If the argument has any other value,
the behavior is undefined."

I am having a hard time formulating my question - basically its like
this though - Some people say cast to unsigned char (as in the above
example), whereas I have seen some people argue that casting to
unsigned char is unecessary, and if it is done, then a recast back to
int is necessary, because functions like toupper() expect an int, eg,

toupper( (int)((unsigned char) c) );

So what is the right thing to do? Cast to unsigned char? Cast to
unsigned char and back to int?



IIRC, the C standard says that all members of the execution characterset
will be expressable as positive values.

Since the use of toupper() only makes sense within the scope of the
execution characterset, and not for arbitrary char values outside of
that range, it is safe to say that toupper() only works on positive char
values, or EOF (which is a specific, often negative, char value).

Casting the parameter to an unsigned char
- - may change the interpretation of the value of the parameter, if it is
(a negative value) EOF
- - has no effect on proper members of the execution character set

Casting this back to int, while properly correcting the type of the
parameter to int, otherwise has (to my knowledge) no other effect. The
damage has been done by the cast to unsigned char, and cannot be
corrected by the recasting to int.

In the end, toupper() was originally meant to take an int (as in the
return value of fgetc(), and that''s what you should give it. If you are
working with char data items, convert them to int first, but otherwise
don''t cast. I.e.
#include <ctype.h>
#include <stdio.h>

{
char datum = ''c'', uc_datum;
int file_datum, uc_file_datum;

file_datum = fgetc(stdin);
uc_file_datum = toupper(file_datum);

uc_datum = toupper((int)datum);
}

- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer''s)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFCwrO+agVFX4UWr64RAn4DAKCnWQEAHo7kXd8xv3DFlJ FIyDH7BQCg5W9M
REN07taxd5C5T4SJMM8JaSk=
=HH+V
-----END PGP SIGNATURE-----




mr**********@hotmail.com wrote:

I have been reading about the practise of casting values to unsigned
char while using the <ctype.h> functions. For example,

c = toupper ((unsigned char) c);

Now I understand that the standard says this about the <ctype.h>
functions:

"The header <ctype.h> declares several functions useful for classifying
and mapping characters.166) In all cases the argument is an int, the
value of which shall be representable as an unsigned char or shall
equal the value of the macro EOF. If the argument has any other value,
the behavior is undefined."

I am having a hard time formulating my question - basically its like
this though - Some people say cast to unsigned char (as in the above
example), whereas I have seen some people argue that casting to
unsigned char is unecessary, and if it is done, then a recast back to
int is necessary, because functions like toupper() expect an int, eg,

toupper( (int)((unsigned char) c) );

So what is the right thing to do? Cast to unsigned char? Cast to
unsigned char and back to int?



If `c'' is a plain `char'', cast it to `unsigned char''.
The further cast to `int'' is harmless but unnecessary: since
<ctype.h> provides a prototype that says toupper() takes an
`int'' argument, the compiler will do the conversion anyhow.

The reason you need the cast is that converting directly
from plain `char'' to `int'' might not produce what toupper()
needs. Specifically, if `char'' is a signed type and `c'' has
a negative value, direct conversion will produce a negative
`int''. If this negative `int'' happens to equal EOF toupper()
will just return the EOF unaltered, and this might not be the
upper-case equivalent of `c''. If the negative `int'' is
something other than EOF, all bets are off and you are in the
perilous realm of Undefined Behavior.

If `c'' is an `int'' obtained from something like getc(),
just pass it along without casting. getc() and its ilk
already return either EOF or a non-negative `unsigned char''
value, which is what toupper() et al. require.

--
Er*********@sun.com


mr**********@hotmail.com wrote:


I have been reading about the practise of casting values to unsigned
char while using the <ctype.h> functions. For example,

c = toupper ((unsigned char) c);

Now I understand that the standard says this about the <ctype.h>
functions:

"The header <ctype.h>
declares several functions useful for classifying
and mapping characters.166) In all cases the argument is an int, the
value of which shall be representable as an unsigned char or shall
equal the value of the macro EOF. If the argument has any other value,
the behavior is undefined."

I am having a hard time formulating my question - basically its like
this though - Some people say cast to unsigned char (as in the above
example), whereas I have seen some people argue that casting to
unsigned char is unecessary, and if it is done, then a recast back to
int is necessary, because functions like toupper() expect an int, eg,

toupper( (int)((unsigned char) c) );

So what is the right thing to do? Cast to unsigned char? Cast to
unsigned char and back to int?



Since toupper is undefined for values which are
not representable as unsigned char,
then if a cast to unsigned char will change the value,
then do that, if not, then don''t bother.

fputc, by which all file output is described,
converts it''s int argument to unsigned char, before output.

So, if you have a negative integer value like:
#define NEG_a (''a'' - 1 - (unsigned char)-1)
you know that
putchar(NEG_a);
will output the ''a'' character.

To make that negative number work with toupper:
putchar(toupper((unsigned char)NEG_a));

--
pete


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

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