十进制常数太大 [英] decimal constant too large

查看:98
本文介绍了十进制常数太大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下C程序:


int main(无效)

{

int x = -2147483648;


返回(0);

}


产生以下警告:


temp.c:在函数main中:

temp.c:3:警告:十进制常量太大,以至于未签名


关于Win98和MVS上的gcc。


据我所知,这实际上是

上的最大负数

环境,因此常量符合有符号整数和

因此我不应该收到警告。


任何想法?我检查了常见问题解答,但没有看到任何内容。


谢谢。保罗。

解决方案

kerravon写道:


以下C程序:


int main(无效)

{

int x = -2147483648;


返回(0);

}


产生以下警告:


temp.c:在函数`main''中:

temp.c:3:警告:十进制常量是如此之大以至于未签名


在Win98和MVS上的gcc 。


据我所知,这实际上是

环境中的最大负数,因此常量符合有符号整数和

因此我不应该收到警告。



`-2147483648`不是十进制常量;这是一种表达方式。有问题的小数

常数是'2147483648`,它比用32位有符号整数表示的大b / b更大。


-

编译器15,OP爱刺猬

花了很长时间,比最慷慨的估计要长得多。

- James White,/ Sector General /


11月10日晚上10:30,Chris Dollin< e ... @ electrichedgehog.netwrote:


kerravon写道:


以下C程序:


int main(void)

{

int x = -2147483648;


return(0);

}


产生以下警告:


temp.c:在函数main中:

temp。 c:3:警告:十进制常量是如此之大,以至于无符号


在Win98和MVS上的gcc上。


据我所知,这实际上是

上的最大负数

环境,因此常量符合有符号整数和

因此我不应该收到警告。



`-2147483648`不是十进制常数;这是一种表达方式。有问题的十进制

常数是'2147483648`,它比用32位有符号整数表示的大b / b
更大。



我明白了。那么解决方案是什么呢?它是-2147483648U?


我可以从其他人的限制中看出他们已经成功了

( - 2147483647-1)。为什么他们会这样做,而不是在最后坚持一个U

?是不是因为最后的U会强制所有的东西

进入无符号算术?


谢谢。保罗。


kerravon< ke ****** @ w3.towrites:


以下C程序:


int main(无效)

{

int x = -2147483648;


返回(0);

}


产生以下警告:


temp.c:在函数`main'':

temp.c:3:警告:十进制常量太大,以至于未签名


关于Win98和MVS上的gcc。


据我所知,这实际上是那些环境中的最大负数,因此不断适合签到

整数,因此我不应该收到警告。



除了C没有负面文字。你的初始化程序是一个表达式:

一元否定运算符应用于常量。那个常数,确实是如此之大,以至于它是无符号的(在C90中)。否定

这个大的无符号数在技术上会引发整数溢出。


达到系统限制的最佳方法是:

#include< limits.h>


在那里你会找到INT_MIN。在我的系统上,它的定义是

(-INT_MAX - 1)。可以看看这是如何避免任何溢出的?


C99有一个微妙的变化 - 数字2147483648必须是

被解释为类型''签名长long int''。两者都因为长期引入了b $ b,但也因为有关常量

被取消签名和未签名的规则发生了变化。因此在C99中有
没有溢出,因为否定的long long int只是适合

int。


显然,在具有其他整数

尺寸的系统上,数字会有所不同。


-

Ben。


The following C program:

int main(void)
{
int x = -2147483648;

return (0);
}

Produces the following warning:

temp.c: In function `main'':
temp.c:3: warning: decimal constant is so large that it is unsigned

on gcc on Win98 and MVS.

As far as I can tell, that is in fact the maximum negative number on
those
environments and therefore the constant fits into a signed integer and
therefore I shouldn''t be getting a warning.

Any ideas? I checked the FAQ but didn''t see anything.

Thanks. Paul.

解决方案

kerravon wrote:

The following C program:

int main(void)
{
int x = -2147483648;

return (0);
}

Produces the following warning:

temp.c: In function `main'':
temp.c:3: warning: decimal constant is so large that it is unsigned

on gcc on Win98 and MVS.

As far as I can tell, that is in fact the maximum negative number on
those
environments and therefore the constant fits into a signed integer and
therefore I shouldn''t be getting a warning.

`-2147483648` isn''t a decimal constant; it''s an expression. The decimal
constant in question is `2147483648`, which is one bigger than can be
expressed in a 32-bit signed integer.

--
Compiler 15, OP love Hedgehog
"It took a very long time, much longer than the most generous estimates."
- James White, /Sector General/


On Nov 10, 10:30 pm, Chris Dollin <e...@electrichedgehog.netwrote:

kerravon wrote:

The following C program:

int main(void)
{
int x = -2147483648;

return (0);
}

Produces the following warning:

temp.c: In function `main'':
temp.c:3: warning: decimal constant is so large that it is unsigned

on gcc on Win98 and MVS.

As far as I can tell, that is in fact the maximum negative number on
those
environments and therefore the constant fits into a signed integer and
therefore I shouldn''t be getting a warning.


`-2147483648` isn''t a decimal constant; it''s an expression. The decimal
constant in question is `2147483648`, which is one bigger than can be
expressed in a 32-bit signed integer.

I see. So is the solution then to make it -2147483648U ?

I can see from other people''s limits.h that they have made it
(-2147483647-1). Why would they do that instead of sticking a U
on the end? Is it because the U on the end will force everything
into unsigned arithmetic?

Thanks. Paul.


kerravon <ke******@w3.towrites:

The following C program:

int main(void)
{
int x = -2147483648;

return (0);
}

Produces the following warning:

temp.c: In function `main'':
temp.c:3: warning: decimal constant is so large that it is unsigned

on gcc on Win98 and MVS.

As far as I can tell, that is in fact the maximum negative number on
those environments and therefore the constant fits into a signed
integer and therefore I shouldn''t be getting a warning.

Except C has no negative literals. Your initializer is an expression:
the unary negation operator is applied to a constant. That constant,
2147483648, is indeed so large that it is unsigned (in C90). Negating
this large unsigned number provokes, technically, integer overflow.

The best way to at you system limits is with:

#include <limits.h>

There you will find INT_MIN. On my system, its definition is
(-INT_MAX - 1). Can see how this avoids any overflow?

There was a subtle change in C99 -- the number 2147483648 must be
interpreted as having type ''signed long long int''. Both because long
long was introduced but also because the rules about what constants
are taken to be signed and unsigned were changed. Thus in C99 there
is no overflow since the negated long long int does, just, fit into an
int.

Obviously the numbers will be different on systems with other integer
sizes.

--
Ben.


这篇关于十进制常数太大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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