奇怪的'C'警告 - 你能解决这个问题吗? [英] weird 'C' warning - can you solve this?

查看:62
本文介绍了奇怪的'C'警告 - 你能解决这个问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在反复编译一些代码而没有任何问题。版本之间唯一的

差异是略有不同的常量,

特定于各种嵌入式设备的加载。

突然我注意到了某个值会产生警告。这是

低位:


-6的特定实例。在这句话中产生一个警告:


const签名长tzerocal_A [] = {80000,15000,-60000}; //不好


有一些实验,-50000和-40000也不喜欢

所有这些其他版本都可编译好

const签名long tzerocal_A [] = {80000,15000,-70000}; //罚款

const签名长tzerocal_A [] = {80000,15000,-80000}; //罚款

a解决方法(我认为)是明确地设置一个L来表示

长 - 但为什么那会起作用,谁知道?


const签名长tzerocal_A [] = {80000,15000,-60000L}; //罚款

警告具体是:


[警告]一元的无符号操作数 -


任何人都可以解释为什么会这样。如果-60000超出了签名长的

有效范围,那么肯定-70000或-80000也会产生警告




Thx。罗斯。

解决方案

" Ross" <我的*********** @ nomailplace.ca>在留言中写道

news:mg ******************************** @ 4ax.com ...


我正在反复编译一些代码而没有任何问题。版本之间唯一的区别是略有不同的常量,这些常量特定于各种嵌入式设备的加载。
突然我注意到某个值会产生警告。这是
低位:

-6的具体实例。在这句话中产生一个警告:

const签名长tzerocal_A [] = {80000,15000,-60000}; //不好

有些实验,-50000和-40000也不喜欢

所有这些其他版本都会编译好
const签名长tzerocal_A [] = {80000,15000,-70000}; //罚款
const签名长tzerocal_A [] = {80000,15000,-80000}; //很好

一个解决方案(我认为)是明确地用一个L来表示长期 - 但为什么那会起作用,谁知道呢?

const签名长tzerocal_A [] = {80000,15000,-60000L}; //罚款

警告具体是:

[警告]一元的无符号操作数 -

任何人都可以解释为什么会这样。如果-60000超出了有效长度的有效范围,那么肯定-70000或-80000也会产生警告。




实际上这很明显。 int在你的系统上是16位宽,所以任何超过

32767的东西都被认为是无符号的。在数字后附加一个明确的L

很长。为了证明这一点,你可以尝试32768和65535之间的任何数字

包括在内,所有这些都应该给你警告。数字较小或比b / b更大的数字似乎没问题。正确的方法当然是使用

L.


Peter


Ross< my * **********@nomailplace.ca>写道:


-6的具体实例在这句话中产生一个警告:

const签名长tzerocal_A [] = {80000,15000,-60000}; // bad
[...] [警告] unary的无符号操作数 -

任何人都可以解释为什么会发生这种情况。




因为你的编译器坏了。它似乎将INT_MAX和UINT_MAX之间的非固定

十进制常量视为类型为unsigned

int,但C标准要求它们具有long int类型(与

八进制和十六进制常量,十进制常量*从不*解释为

未签名,没有明确的u或U后缀)。


-Larry琼斯


外科医生应该发出关于和女孩玩耍的警告。 - Calvin


Peter Pichler写道:


Ross <我的*********** @ nomailplace.ca>在消息中写道
新闻:mg ******************************** @ 4ax.com ... < blockquote class =post_quotes>
我正在反复编译一些代码而没有任何问题。版本之间唯一的区别是略有不同的常量,这些常量特定于各种嵌入式设备的加载。
突然我注意到某个值会产生警告。这是
低位:

-6的具体实例。在这句话中产生一个警告:

const签名长tzerocal_A [] = {80000,15000,-60000}; //不好

有些实验,-50000和-40000也不喜欢

所有这些其他版本都会编译好
const签名长tzerocal_A [] = {80000,15000,-70000}; //罚款
const签名长tzerocal_A [] = {80000,15000,-80000}; //很好

一个解决方案(我认为)是明确地用一个L来表示长期 - 但为什么那会起作用,谁知道呢?

const签名长tzerocal_A [] = {80000,15000,-60000L}; //罚款

警告具体是:

[警告]一元的无符号操作数 -

任何人都可以解释为什么会这样。如果-60000超出有效长度的有效范围,那么肯定-70000或-80000也会产生警告。



它是显而易见的。 int在您的系统上是16位宽,因此任何超过
32767的东西都被视为无符号。




然后编译器坏了。如果小于LONG_MAX,那么超过32767的未加十进制的十进制常量必须被认为是``long int'''',假设一个实现

,其中INT_MAX是32767。 />



I''m compiling some code over and over with no problems. The only
differences between the versions is slightly different constants that
are specific to various embedded devices that are getting loaded.
Suddenly I notice a certain value generates a warning. Here''s the
low-down:

The specific instance of the "-6" in this phrase generates a warning:

const signed long tzerocal_A[] ={80000, 15000, -60000 }; //bad

with some experimenting, -50000, and -40000 are also not liked
All these other versions will compile fine
const signed long tzerocal_A[] ={80000, 15000, -70000 }; //fine
const signed long tzerocal_A[] ={80000, 15000, -80000 }; //fine
a work around (I think) is to explicitly put an L in place to denote a
long - but why that would work, who knows?

const signed long tzerocal_A[] ={80000, 15000, -60000L }; //fine
The warning is specifically:

[warning] unsigned operand of unary -

Can anyone explain why this is happening. If -60000 were beyond the
valid range of a signed long, then surely -70000 or -80000 would
generate a warning too.

Thx. Ross.

解决方案

"Ross" <my***********@nomailplace.ca> wrote in message
news:mg********************************@4ax.com...


I''m compiling some code over and over with no problems. The only
differences between the versions is slightly different constants that
are specific to various embedded devices that are getting loaded.
Suddenly I notice a certain value generates a warning. Here''s the
low-down:

The specific instance of the "-6" in this phrase generates a warning:

const signed long tzerocal_A[] ={80000, 15000, -60000 }; //bad

with some experimenting, -50000, and -40000 are also not liked

All these other versions will compile fine
const signed long tzerocal_A[] ={80000, 15000, -70000 }; //fine
const signed long tzerocal_A[] ={80000, 15000, -80000 }; //fine

a work around (I think) is to explicitly put an L in place to denote a
long - but why that would work, who knows?

const signed long tzerocal_A[] ={80000, 15000, -60000L }; //fine

The warning is specifically:

[warning] unsigned operand of unary -

Can anyone explain why this is happening. If -60000 were beyond the
valid range of a signed long, then surely -70000 or -80000 would
generate a warning too.



It''s obvious actually. int is 16 bits wide on your system, so anything over
32767 is considered unsigned. Appending an explicit L after the number makes
it long. To prove that, you could try any number between 32768 and 65535
inclusive, all of them should give you the warning. Numbers smaller or
bigger than that would appear to be OK. The correct way is of course using
L.

Peter


Ross <my***********@nomailplace.ca> wrote:


The specific instance of the "-6" in this phrase generates a warning:

const signed long tzerocal_A[] ={80000, 15000, -60000 }; //bad [...] [warning] unsigned operand of unary -

Can anyone explain why this is happening.



Because your compiler is broken. It appears to be treating unsuffixed
decimal constants between INT_MAX and UINT_MAX as having type unsigned
int, but the C Standard requires them to have type long int (unlike
octal and hex constants, decimal constants are *never* interpreted as
unsigned without an explicit u or U suffix).

-Larry Jones

The surgeon general should issue a warning about playing with girls. -- Calvin


Peter Pichler wrote:


"Ross" <my***********@nomailplace.ca> wrote in message
news:mg********************************@4ax.com...


I''m compiling some code over and over with no problems. The only
differences between the versions is slightly different constants that
are specific to various embedded devices that are getting loaded.
Suddenly I notice a certain value generates a warning. Here''s the
low-down:

The specific instance of the "-6" in this phrase generates a warning:

const signed long tzerocal_A[] ={80000, 15000, -60000 }; //bad

with some experimenting, -50000, and -40000 are also not liked

All these other versions will compile fine
const signed long tzerocal_A[] ={80000, 15000, -70000 }; //fine
const signed long tzerocal_A[] ={80000, 15000, -80000 }; //fine

a work around (I think) is to explicitly put an L in place to denote a
long - but why that would work, who knows?

const signed long tzerocal_A[] ={80000, 15000, -60000L }; //fine

The warning is specifically:

[warning] unsigned operand of unary -

Can anyone explain why this is happening. If -60000 were beyond the
valid range of a signed long, then surely -70000 or -80000 would
generate a warning too.



It''s obvious actually. int is 16 bits wide on your system, so anything over
32767 is considered unsigned.



Then the compiler is broken. The unsuffixed decimal constants over 32767 must be
considered ``long int'''' if it is less than LONG_MAX, assuming an implementation
where INT_MAX is 32767.


这篇关于奇怪的'C'警告 - 你能解决这个问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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