Microchip
直播中

黄明光

8年用户 331经验值
私信 关注
[问答]

带有无符号字符的隐式类型转换

大家好,我正在写一个需要使用无符号字符的程序。但是,每当我使用一个算术运算符或位运算符时,总是会得到“隐式签名到无符号转换”警告。结果是,在没有警告的情况下生成以下内容:例如:无符号CHAR测试(unChar char Test1,unChar TestStay2){Realth1(Test1)}我必须如下:无符号char测试(unChar char est1,unchar char est2){返回((unChar char)(TESTE1+Test2));}这不是必须的,不是吗?我唯一能想到的是,我的OrxC8项目已经设置了一些非常苛刻的语法设置。任何建议都将非常感激,因为不断地需要抛出这两个问题,并导致一些非常笨拙的源代码。格雷姆。

以上来自于百度翻译


      以下为原文

    Hi all,

I'm writing a program which requires the use of unsigned chars. However, I'm constantly getting "implicit signed to unsigned conversion" warnings whenever I use an arithmetic or bitwise operator on one.

The result is that to get the following to build without a warning, for example:

    unsigned char test(unsigned char test_1, unsigned char test_2) {
        return (test_1 + test_2);
    }

I have to cast as follows:

    unsigned char test(unsigned char test_1, unsigned char test_2) {
        return ((unsigned char)(test_1 + test_2));
    }

This can't be necessary, can it?   The only thing I can think is that I've got my project or XC8 set up with some extremely draconian syntax settings.

Any advice would be greatly appreciated as the constant need to cast is both concerning and leads to some very unwieldy source code.

Graeme.

回帖(12)

蒋喜玲

2018-10-25 17:12:26
可以降低警告级别。XC8的最新版本提高了默认警告级别。一致认为这是恼人的,但是标准的C整数提升是原因。你可以让它不那么令人讨厌,因为包含& lt;Stdit.h & gt;然后,你可以使用标准类型的快捷方式,比如“uut88t”,它比“unChar char”稍微有点侵入。

以上来自于百度翻译


      以下为原文

    You can reduce the warning level. Recent versions of XC8 upped the default warning level. Agreed that it is annoying, but standard C integer promotions are the cause.
 
You can make it a little less unpleasant by #include - then you can use the standard type shortcuts like "uint8_t" which is a little less intrusive than "unsigned char".
举报

王璨

2018-10-25 17:18:17
欢迎来到“C傻瓜”俱乐部(只是戏弄)。C中的大多数算术运算都有一个整数结果(隐式定义)。因此。使用STNDIT.H很少有助于减轻这种影响。但是,由于变量的大小更容易识别,这使得代码更可读。

以上来自于百度翻译


      以下为原文

    Welcome to the club of "C dummies" (just teasing).
 
Most arithmetic operations in C have an integer result (implicitly defined). THUS . . .
And the use of stdint.h serves little to mitigate this effect. But it makes the code more readable as the size of the variables is easier to discern.
举报

李俊一

2018-10-25 17:25:04
是的,我知道“C”中的默认类型的int。更多的是困扰我的警告。我刚刚从MikroC移到XC8,其中我一直使用8位数据,没有任何隐式转换警告。我不愿意降低警告水平,因为我可能会错过一些潜在的吼叫,所以我一定会开始使用UTI88T来保持整洁。

以上来自于百度翻译


      以下为原文

    Yes, I know about the default type of int in 'C'. It's more the plethora of warnings that was bothering me. I've just moved over to XC8 from MikroC, with which I used 8 bit data all the time without any kind of implicit conversion warning. I'm reluctant to reduce the warning level though because I could miss some potential howlers, so I'll definitely start using uint8_t to keep things tidy.
举报

王璨

2018-10-25 17:38:37
XC8&公司最近才成熟起来——包括多种类型的冲突警告。虽然这需要大量明确的类型转换来消除大部分(并非全部)的警告,但我认为类型转换是程序员成熟的标志:显示S/He知道O。不冲突的ANSI标准仍然允许各种关于警告的实现。并不是每一个编译器生产商都为ANSI标记而努力。(加上:像我这样的老狗仍然喜欢前ANSI K & R”标准。

以上来自于百度翻译


      以下为原文

    It's only recently that XC8 & Co. matured significantly - including a multitude of type conflict warnings.
Although this requires a lot of explicit type casting to get rid of most (not all) of the warnings, I consider type casts as some sign of maturity of the programmer: showing s/he is aware of the conflicts.
 
The unfamous ANSI standard still allows for a variety of implementations regarding the warnings. And not every compiler producer even strives for the ANSI labeling.
(Plus: some old dogs like me still love the pre-ANSI K&R "standard".)
举报

更多回帖

发帖
×
20
完善资料,
赚取积分