直接计算K& R2中的整数限制? [英] Direct computation of integer limits in K&R2?

查看:38
本文介绍了直接计算K& R2中的整数限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在K& R2中,一个练习要求读者计算并打印基本整数类型的限制

。对于无符号类型,这是微不足道的。但是,对于没有调用未定义行为的签名类型,它是否可以


由溢出触发?请记住,limits.h中的常量不能使用


Hello all,

In K&R2 one exercise asks the reader to compute and print the limits for
the basic integer types. This is trivial for unsigned types. But is it
possible for signed types without invoking undefined behaviour
triggered by overflow? Remember that the constants in limits.h cannot
be used.

推荐答案

santosh写道:
santosh wrote:

大家好,

在K& R2中,一个练习要求读者计算并打印
$的限制b $ b基本整数类型。对于无符号类型,这是微不足道的。但是,对于没有调用未定义行为的签名类型,它是否可以


由溢出触发?请记住,limits.h中的常量不能使用

Hello all,

In K&R2 one exercise asks the reader to compute and print the limits for
the basic integer types. This is trivial for unsigned types. But is it
possible for signed types without invoking undefined behaviour
triggered by overflow? Remember that the constants in limits.h cannot
be used.



根据无符号类型计算这个是不可能的

相同尺寸?


-

Ian Collins。

Isn''t it possible to calculate this based on the unsigned types of the
same size?

--
Ian Collins.


Ian Collins写道:
Ian Collins wrote:

santosh写道:
santosh wrote:

> Hello all,
在K& R2中,一项练习要求读者计算并打印基本整数类型的限制。对于无符号类型,这是微不足道的。但是,如果没有调用溢出触发的未定义行为,签名类型是否可能?请记住,limits.h中的常量不能被使用。
>Hello all,

In K&R2 one exercise asks the reader to compute and print the limits
for the basic integer types. This is trivial for unsigned types. But
is it possible for signed types without invoking undefined behaviour
triggered by overflow? Remember that the constants in limits.h cannot
be used.



根据
大小相同?

Isn''t it possible to calculate this based on the unsigned types of the
same size?



这不需要知道所使用的编码,是否两个

补码或符号和幅度等?

Won''t this require knowledge of the encoding used, whether twos
complement or sign and magnitude etc?


2008年3月12日星期三03:07:48 +0530,santosh写道:
On Wed, 12 Mar 2008 03:07:48 +0530, santosh wrote:

大家好,


在K& R2中,一个练习要求读者计算并打印基本整数类型的限制。对于无符号类型,这是微不足道的。但是,对于没有调用未定义行为的已签名类型,可能是
吗?溢出会触发
?请记住,limits.h中的常量不能使用。
Hello all,

In K&R2 one exercise asks the reader to compute and print the limits for
the basic integer types. This is trivial for unsigned types. But is it
possible for signed types without invoking undefined behaviour triggered
by overflow? Remember that the constants in limits.h cannot be used.



#include< stdio.h>

int main(void){

unsigned u = - 1;

int i;

while((i = u)< 0 || i!= u)

u = u> 1;

printf(" INT_MAX ==%u \ n,u);

}


这是不能保证在C99中工作,其中转换一个超出

范围的整数可能会产生一个信号,但它是有效的C90,因为
$ b $的结果b转换必须是有效的int,因此在INT_MIN和

INT_MAX之间。

#include <stdio.h>
int main(void) {
unsigned u = -1;
int i;
while ((i = u) < 0 || i != u)
u = u >1;
printf("INT_MAX == %u\n", u);
}

This is not guaranteed to work in C99, where the conversion of an out-of-
range integer may raise a signal, but it''s valid C90, since the result of
the conversion must be a valid int, and therefore between INT_MIN and
INT_MAX.


这篇关于直接计算K&amp; R2中的整数限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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