增加变量超过限制 [英] Incrementing variables past limits

查看:105
本文介绍了增加变量超过限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


ANSI标准是否说过将变量递增到过去的限制?


当我编译这样的代码时:


unsigned char x = 255;

x ++;

printf("%d \ n",x);


与GCC,输出为0.


从装配的角度来看,这似乎是逻辑行为,但它是在标准中定义的,或者它是否依赖于实现?


我已经用Google搜索了答案,请阅读K& R2和clc常见问题但无法找到

任何体面的答案..

提前致谢,

Bas Wassink。

Hi there,

Does the ANSI standard say anything about incrementing variables past
their limits ?

When I compile code like this:

unsigned char x = 255;
x++;
printf ( "%d\n", x );

with GCC, the output is 0.

From an assembly point of view this seems to be logical behaviour, but is
it defined in the standard, or is it implementation dependant ??

I''ve googled for an answer, read K&R2 and the c.l.c FAQ but couldn''t find
any decent answer..
Thanks in advance,
Bas Wassink.

推荐答案

Bas Wassink写道:
Bas Wassink wrote:
你好,

ANSI标准是否说关于增加变量超过其限制的任何事情?

当我编译这样的代码时:

unsigned char x = 255;
x ++;
printf("%d \ n",x);

使用GCC,输出为0.

从汇编的角度来看这似乎是逻辑行为,但它是在标准中定义的,还是依赖于实现?

我已经google了一个答案,阅读K& R2和clc常见问题,但无法找到任何合适的答案..
Hi there,

Does the ANSI standard say anything about incrementing variables past
their limits ?

When I compile code like this:

unsigned char x = 255;
x++;
printf ( "%d\n", x );

with GCC, the output is 0.

From an assembly point of view this seems to be logical behaviour, but is
it defined in the standard, or is it implementation dependant ??

I''ve googled for an answer, read K&R2 and the c.l.c FAQ but couldn''t find
any decent answer..




结果是无符号整数的明确定义:它们

遵守模块化(时钟)算术的规则。


对于无符号整数,没有保证。

程序可能会陷阱,或者可能会提供一些实现定义的

结果。大多数实现环绕从最好的
积极到最负面的价值,但C语言

本身并不承诺这种行为。


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



The result is well-defined for unsigned integers: they
obey the rules of modular ("clock") arithmetic.

For unsigned integers, there are no guarantees. The
program may trap, or may deliver some implementation-defined
result. Most implementations "wrap around" from the most
positive to the most negative value, but the C language
itself doesn''t promise this behavior.

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


Bas Wassink< id ****** @ email.org>写道:
Bas Wassink <id******@email.org> writes:
ANSI标准是否说过将变量增加到其限制以外的任何内容?
Does the ANSI standard say anything about incrementing variables past
their limits ?




是的。无符号整数类型环绕。对于其他类型,它是不可预测的,你应该避免与他们进行越界算术



-

我们把[最好的]汇编程序员放在走廊里的一个小玻璃柜里

靠近出口标志。案件上的标志上写着,如果是优化

问题,请打破玻璃。同时,问题解决者正在忙于用最适合的语言工作

手头的工作。 --Richard Riehle



Yes. Unsigned integer types wrap around. With other types it''s
unpredictable and you should avoid doing out-of-bounds arithmetic
with them.
--
"We put [the best] Assembler programmers in a little glass case in the hallway
near the Exit sign. The sign on the case says, `In case of optimization
problem, break glass.'' Meanwhile, the problem solvers are busy doing their
work in languages most appropriate to the job at hand." --Richard Riehle


< cr ********** @ news1brm.Central.Sun.COM>

Eric Sosman< er ********* @ sun.com>写道:
<cr**********@news1brm.Central.Sun.COM>
Eric Sosman <er*********@sun.com> wrote:
对于无符号整数,没有保证。
^^^^^^^^(我知道你的意思是签名:)程序可能会陷阱,或者可能会提供一些实现定义的结果。大多数实现环绕从最积极的价值到最负面的价值,但C语言本身并不承诺这种行为。
For unsigned integers, there are no guarantees. The ^^^^^^^^ (I know you meant "signed" :) program may trap, or may deliver some implementation-defined
result. Most implementations "wrap around" from the most
positive to the most negative value, but the C language
itself doesn''t promise this behavior.




-

Christopher Benson-Manica

ataru(at)cyberspace.org



--
Christopher Benson-Manica
ataru(at)cyberspace.org


这篇关于增加变量超过限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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