有符号和无符号字符有什么区别? [英] What is the difference between signed and unsigned char?

查看:143
本文介绍了有符号和无符号字符有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好C级程序员,


我正在学习用C编程,我想知道

之间的区别是什么和unsigned char是。对我来说似乎没有区别,并且标准甚至不关心普通字符是什么(因为签名和无符号具有相同的行为)。 br />

例如,如果有人这样做:


unsigned char a = -2; / *或= 254 * /

签名char b = -2; / *或= 254 * /


putchar(a);

putchar(b); / *都打印相同的字符(ex ascii 254)* /


-------------

似乎我无论char是签名还是

无符号都没关系,因为输出函数只是查看位模式而

将它作为正数处理。

另外,我为unsigned char分配了一个负数,它包裹了

并创建了与分配相同负数相同的位模式

签名char。


所以我的问题是,unsigned和

签名字符之间的区别是什么?


另外,对于其他整数类型,正常类型总是等于

签名类型(int = signed int,long = signed long等等)...或者

是为chars定义的实现?

任何帮助都将受到赞赏。

解决方案



tine ... @ gmail.com写道:

H ello C程序员,

我只是学习用C编程,我想知道有符号和无符号字符之间的区别是什么。对我来说似乎没有区别,标准甚至不关心普通的
char是什么(因为签名和无签名具有相同的行为)。

例如,如果有人这样做:

unsigned char a = -2; / *或= 254 * /
签名字符b = -2; / *或= 254 * /




我不认为你可以为签名的

整数分配一个否定的初始值。

我是对的人吗?


反过来......

unsigned char没有标志延长。

下午1/27/05 4:21 PM,文章
11 ********************* @ c13g2000cwb.googlegroups.c om , Kobu

< ko ******** @ gmail.com>写道:


tine ... @ gmail.com写道:

你好C程序员,

我我正在学习用C编程,我想知道有符号和无符号字符之间的区别是什么。对我来说似乎没有区别,标准甚至不关心普通的


char

是什么(因为有符号和无符号有相同的行为)。

例如,如果有人这样做:

unsigned char a = -2; / *或= 254 * /
签名字符b = -2; / *或= 254 * /



我认为你不能为一个签名的
整数分配一个否定的初始值。
我是对的人吗?




< ti ***** @ gmail.com>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...

我只是在学习用C编程,我想知道有符号和无符号字符的区别是什么。对我而言,似乎没有区别,标准甚至不关心普通的char是什么(因为签名和无签名具有相同的行为)。

例如,如果有人这样做:

unsigned char a = -2; / *或= 254 * /


在此,类型为int的值-2将转换为unsigned char。这个

转换被指定为相当于重复添加或

减去UCHAR_MAX + 1(其中UCHAR_MAX是无符号的最大值

char可以你的编译器显然是255;直到结果介于

0和UCHAR_MAX之间。所以a被赋值为254.

signed char b = -2; / *或= 254 * /


这里,类型为int的-2再次转换为signed char。但请注意

将b分配给b的效果(可以保持SCHAR_MIN

和SCHAR_MAX之间的值,在你的情况下分别为-128和127)

标准未定义。

putchar(a);
putchar(b); / *都打印相同的字符(ex ascii 254)* /


putchar函数接受一个int,因此对于这两个调用,参数是

转换为int类型;这些调用分别相当于putchar(254)和

putchar(-2)。 putchar函数被指定为将其参数转换为unsigned char,后者使用上述规则。因此,当

UCHAR_MAX为255时,第二次调用相当于第一次调用

结果。

在我看来无论char是签名还是无符号都无关紧要,因为输出函数只是查看位模式并将其作为正数处理。


见上文。

另外,我为unsigned char分配了一个负数,它包裹了
并创建了与分配的相同的位模式相同的负数
签名char。


请参阅上面的规则。环绕是标准规定的。

事实它是相同的位模式是常见的,因为签名数字的两个'补码

表示很常见,但是两个'的补码不是

标准要求。

所以我的问题是,unsigned和
签名char之间的区别是什么?


一个可以表示无符号值,其他符号值(显然)。

如上所述,将超出范围的值转换为带符号的值(例如,签名字母为
)标准未定义。类似地,生成该类型的超出范围值的算法的结果是未定义的。但是

这两种情况下对于无符号类型的行为(例如unsigned char)

*是*定义的。

另外,对于其他积分类型,常规类型总是等于签名类型(int = signed int,long = signed long等等)......或者
是实现定义的类似于字符吗? / blockquote>


char可以表示与signed char或

unsigned char相同的值范围,但这三个都是不同的类型。 (类似地,int和long

可能能够代表给定实现的相同范围的值,

但它们也是不同的类型。)


int,short,long(以及C99中的long long)始终能够表示

的负数。我认为int和signed int是相同的类型,并且

类似于short,long和long long。希望其他人可以澄清

这一点。


HTH,

Alex

Hello fellow C programmers,

I''m just learning to program with C, and I''m wondering what the
difference between signed and unsigned char is. To me there seems to
be no difference, and the standard doesn''t even care what a normal char
is (because signed and unsigned have equal behavior).

For example if someone does this:

unsigned char a = -2; /* or = 254 */
signed char b = -2; /* or = 254 */

putchar(a);
putchar(b); /* both print the same character (ex ascii 254)*/

-------------
It seems to me that it doesn''t matter whether char is signed or
unsigned, because the output functions just look at the bit pattern and
deal with it as a positive number.
Also, I assigned a negative number to unsigned char, it wraps around
and creates the same bit pattern as assigning the same negative number
to signed char.

So my question is, what really is the difference between unsigned and
signed char?

Also, for other integral types, are the normal types always equal to
the signed types (int = signed int, long = signed long,etc. etc.)... or
is that implementation defined just like for chars?
Any help will be appreciated.

解决方案


tine...@gmail.com wrote:

Hello fellow C programmers,

I''m just learning to program with C, and I''m wondering what the
difference between signed and unsigned char is. To me there seems to
be no difference, and the standard doesn''t even care what a normal char is (because signed and unsigned have equal behavior).

For example if someone does this:

unsigned char a = -2; /* or = 254 */
signed char b = -2; /* or = 254 */



I don''t think you can assign a negative initializer to a signed
integer.
Am I right people?


The other way around...
unsigned char does not have a sign extension.
On 1/27/05 4:21 PM, in article
11*********************@c13g2000cwb.googlegroups.c om, "Kobu"
<ko********@gmail.com> wrote:


tine...@gmail.com wrote:

Hello fellow C programmers,

I''m just learning to program with C, and I''m wondering what the
difference between signed and unsigned char is. To me there seems to
be no difference, and the standard doesn''t even care what a normal


char

is (because signed and unsigned have equal behavior).

For example if someone does this:

unsigned char a = -2; /* or = 254 */
signed char b = -2; /* or = 254 */



I don''t think you can assign a negative initializer to a signed
integer.
Am I right people?




<ti*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

I''m just learning to program with C, and I''m wondering what the
difference between signed and unsigned char is. To me there seems to
be no difference, and the standard doesn''t even care what a normal char
is (because signed and unsigned have equal behavior).

For example if someone does this:

unsigned char a = -2; /* or = 254 */
In this, the value -2, of type int, is converted to unsigned char. This
conversion is specified as being equivalent to repeatedly adding or
subtracting UCHAR_MAX + 1 (where UCHAR_MAX is the maximum value an unsigned
char can have; apparently 255 for your compiler) until the result is between
0 and UCHAR_MAX inclusive. So a is assigned the value 254.
signed char b = -2; /* or = 254 */
Here, -2, again of type int, is converted to signed char. Note however that
the effect of assigning 254 to b (which can hold values between SCHAR_MIN
and SCHAR_MAX inclusive, probably -128 and 127 respectively in your case) is
undefined by the standards.
putchar(a);
putchar(b); /* both print the same character (ex ascii 254)*/
The putchar function takes an int, so for both these calls, the argument is
converted to type int; the calls are equivalent to putchar(254) and
putchar(-2) respectively. The putchar function is specified as converting
its parameter to unsigned char, which uses the rule above. Therefore, with
UCHAR_MAX being 255, the second call is equivalent to the first in terms of
the result.
It seems to me that it doesn''t matter whether char is signed or
unsigned, because the output functions just look at the bit pattern and
deal with it as a positive number.
See above.
Also, I assigned a negative number to unsigned char, it wraps around
and creates the same bit pattern as assigning the same negative number
to signed char.
See the rule above. The wrapping around is what the standards specify. The
fact it is the same bit pattern is common, because two''s complement
representation for signed numbers is common, but two''s complement is not
required by the standards.
So my question is, what really is the difference between unsigned and
signed char?
One can represent unsigned values, and the other signed values (obviously).
As indicated above, conversion of out-of-range values to signed types (such
as signed char) is undefined by the standard. Similarly, the result of
arithmetic that produces out-of-range values for the type is undefined. But
the behaviour in both these cases for unsigned types (such as unsigned char)
*is* defined.
Also, for other integral types, are the normal types always equal to
the signed types (int = signed int, long = signed long,etc. etc.)... or
is that implementation defined just like for chars?



char can represent the same range of values as either signed char or
unsigned char, but all three are distinct types. (Similarly, int and long
may be able to represent the same range of values on a given implementation,
but they too are distinct types.)

int, short, long (and long long in C99) are always capable of representing
negative numbers. I think that int and signed int are the same type, and
similarly for short, long and long long. Hopefully someone else can clarify
this point.

HTH,
Alex


这篇关于有符号和无符号字符有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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