C 中的无符号和有符号值

发布于 2024-11-25 12:16:07 字数 877 浏览 0 评论 0原文

问题是:

在《计算机系统:程序员的视角》一书中的 2.2.5 节中,

它说如果无符号值与有符号值进行比较,则所有值都将以 unsnged 格式进行比较。像这样

if(-1 < 0u)
{
    // will not print this line because -1 will be translated to 255.
    printf("all changed to unsigned format");
}

我在VC6 SP6中尝试了这段代码,字符串没有输出。一切看起来都很好,因为我们都知道 -1 被翻译为 255。

但是当我读到《专家 C 编程深度 C 秘密》一书的第 1.10 节时,

它说如果我的编译器使用 ANSI C 标准,这段代码将打印“- 1 < (unsigned char)1: ANSI":

if(-1 < (unsigned char)1)
{
    printf("-1 < (unsigned char)1: ANSI");
}
else
{
    printf("-1 NOT Less than (unsigned char)1: K&R");    
}

我得到的输出是:-1 < (无符号字符)1:ANSI。

我正在使用 VC6 SP6 编译器。

为什么会发生这种情况?

根据《计算机系统:程序员的视角》一书

-1 < (unsigned char)1 将使 -1 被转换为无符号值。所以它会变成这样:

255 < 1

并且这不应该打印出 -1 < 行(无符号字符)1:ANSI。

谁能告诉我为什么会发生这种情况?

here is the question:

in the Book 'Computer Systems: A Programmer’s Perspective' section 2.2.5

it said that if an unsigned value is comparing with a signed value, all values will be compared in the unisnged format. like this

if(-1 < 0u)
{
    // will not print this line because -1 will be translated to 255.
    printf("all changed to unsigned format");
}

I tried this code in VC6 SP6, the string was NOT outputed. And everything looks good because we all know that -1 was translated to 255.

but when I read the book 'Expert C Programming Deep C Secrets' section 1.10

It said that if my complier uses the ANSI C Standard, this code will print "-1 < (unsigned char)1: ANSI":

if(-1 < (unsigned char)1)
{
    printf("-1 < (unsigned char)1: ANSI");
}
else
{
    printf("-1 NOT Less than (unsigned char)1: K&R");    
}

The output I got was: -1 < (unsigned char)1: ANSI.

I'm using the VC6 SP6 complier.

and why is this happening?

according to the book 'Computer Systems: A Programmer’s Perspective'

-1 < (unsigned char)1 will make -1 be translated as an unsigned value. so it will become sth like this:

255 < 1

and this should not print out the line -1 < (unsigned char)1: ANSI.

can anybody tell me why this is happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七七 2024-12-02 12:16:07

if(-1 < (unsigned char)1)

在这种情况下,两个操作数都提升为 int

if( -1 < 0u )

在这种情况下,两个操作数都转换为 unsigned int

下面的引用证明我是对的。

许多二元运算符期望算术或的操作数
枚举类型会导致转换并以类似的方式产生结果类型
方式。目的是产生一个通用类型,这也是
结果。这种模式称为通常的算术转换,
其定义如下:
— 如果任一操作数的类型为 long
double,另一个应转换为 long double。
—否则,如果
任一操作数为 double,另一个应转换为 double。

否则,如果其中一个操作数是浮点型,则另一个应转换为
漂浮。
— 否则,应执行积分促销 (4.5)
在两个操作数上。54)
— 那么,如果任一操作数是 unsigned long
other 应转换为 unsigned long。
— 否则,如果一个操作数
是一个 long int 和另一个 unsigned int,那么如果一个 long int 可以
表示无符号整型的所有值,无符号整型应为
转换为长整型;否则两个操作数都应转换为
无符号长整数。
— 否则,如果其中一个操作数很长,则另一个
应转换为长。
— 否则,如果任一操作数是
无符号,其他均转为无符号。 [注:否则,
唯一剩下的情况是两个操作数都是 int ]

并且这个(积分提升):

类型为 char、signed char、unsigned char、short int 或的右值
unsigned Short int 可以转换为 int 类型的右值(如果 int)
可以表示源类型的所有值;否则,源
右值可以转换为 unsigned int 类型的右值。

老实说,这些引号来自 C++03 标准,但它们也适用于 C。

if(-1 < (unsigned char)1)

In this case both operands are promoted to int.

if( -1 < 0u )

In this case both operands are converted to unsigned int.

The following quotes prove me right.

Many binary operators that expect operands of arithmetic or
enumeration type cause conversions and yield result types in a similar
way. The purpose is to yield a common type, which is also the type of
the result. This pattern is called the usual arithmetic conversions,
which are defined as follows:
— If either operand is of type long
double, the other shall be converted to long double.
— Otherwise, if
either operand is double, the other shall be converted to double.

Otherwise, if either operand is float, the other shall be converted to
float.
— Otherwise, the integral promotions (4.5) shall be performed
on both operands.54)
— Then, if either operand is unsigned long the
other shall be converted to unsigned long.
— Otherwise, if one operand
is a long int and the other unsigned int, then if a long int can
represent all the values of an unsigned int, the unsigned int shall be
converted to a long int; otherwise both operands shall be converted to
unsigned long int.
— Otherwise, if either operand is long, the other
shall be converted to long.
— Otherwise, if either operand is
unsigned, the other shall be converted to unsigned. [Note: otherwise,
the only remaining case is that both operands are int ]

And this (integral promotions):

An rvalue of type char, signed char, unsigned char, short int, or
unsigned short int can be converted to an rvalue of type int if int
can represent all the values of the source type; otherwise, the source
rvalue can be converted to an rvalue of type unsigned int.

To be perfectly honest, the quotes are from the C++03 standard, but they hold for C as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文