C语言 警告消息:大整数隐式截断为无符号类型 [-Woverflow]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39738497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:29:07  来源:igfitidea点击:

Warning message: large integer implicitly truncated to unsigned type [-Woverflow]

c

提问by Sha

Written a function with unsigned chararguments as shown below

编写一个带unsigned char参数的函数,如下所示

void Address_set(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2)

The above function is called in main()function of C Code as

上述函数在main()C 代码的函数中调用为

Address_set(0,0,239,319);

I received a warning as

我收到了警告

large integer implicitly truncated to unsigned type [-Woverflow]

大整数隐式截断为 unsigned type [-Woverflow]

How to avoid this warning.

如何避免此警告。

回答by David Ranieri

The range of unsigned charis [0 ... 255], the value 319 is truncated (wrap-around) to 319 % 256 = 63

的范围unsigned char是 [0 ... 255],值 319 被截断(环绕)为319 % 256 = 63

How to avoid this warning.

如何避免此警告。

The value is still truncated even if you can avoid this warning using a cast:

即使您可以使用强制转换避免此警告,该值仍会被截断:

Address_set(0,0,239,(unsigned char)319);

Use a longer type, i.e: unsigned short

使用更长的类型,即: unsigned short