PIC XC8 signed char literal constants - Page 1
Forum section: electronics / general

author: NivagSwerdna
author_rank: Super Contributor
timestamp: May 18, 2017, 03:01:17 pm
content: It's only an annoyance but when using XC8...signed char myThing;myThing = -1;The assignment gives a warning due to shortening of the right hand side.How do I declare an 8-bit -1 value to avoid the warning?Thanks in advance

author: Kalvin
author_rank: Super Contributor
timestamp: May 18, 2017, 03:27:40 pm
content: -1 is of type signed integer, so assigning it to signed char will truncate is, thus the warning.You can cast is explicitly to a signed char:Code:myThing = (signed char) -1;Yes, this is very annoying, and it will break the code should you change the type of the myThing.Some compilers support typeof() function, but unfortunately the XC8 doesn't support that.However, there is a better way to do it:Code:typedef signed char myThing_t;myThing_t myThing;myThing = (myThing_t) -1;

author: NivagSwerdna
author_rank: Super Contributor
timestamp: May 18, 2017, 03:38:31 pm
content: But the cast doesn't make the warning go away?

author: Kalvin
author_rank: Super Contributor
timestamp: May 18, 2017, 03:43:38 pm
content: Have you tried it?

author: JPortici
author_rank: Super Contributor
timestamp: May 18, 2017, 04:50:51 pm
content: compiler version?i recall having problems with signed chars not being handled as signed, i solved by using a typedef. crazy, whatever.so, why don't you include <stdint.h> and use the int8_t type? (yeah it just typedef signed char int8_t but you have a standard implementation across compilers and project)or, assuming you don't run out of memory screw it and use an int

author: NivagSwerdna
author_rank: Super Contributor
timestamp: May 18, 2017, 08:33:40 pm
content: Well I tried a few things... cast to (signed char) doesn't work...main.c:299: warning: (752) conversion to shorter data typeand also...typedef volatile signed char myType;myType upBtnCount = 0;...upBtnCount = (myType)-1;gives...main.c:299: warning: (752) conversion to shorter data typebut a slightly more creative... ((signed char)0)-1 gets rid of the warning and generates the correct code (same as a line with a warning)! upBtnCount = (signed char)-1;0x3E2: SETF upBtnCount, ACCESS! downBtnCount = ((signed char)0)-1;0x3E4: SETF downBtnCount, ACCESSi.e. XC 1.36WARNING upBtnCount = (signed char)-1;WARNING upBtnCount = -1;NO WARNING upBtnCount = ((signed char)0)-1;So I can ignore the warnings or I can do some jiggery pokery. Very odd but harmless.

author: NivagSwerdna
author_rank: Super Contributor
timestamp: May 18, 2017, 09:23:24 pm
content: I foundhttp://www.microchip.com/forums/m815346.aspxSeems to be a feature. I'll add a #pragma

author: cv007
author_rank: Frequent Contributor
timestamp: May 18, 2017, 09:34:01 pm
content: Tried it, no warnings for me- XC8 version 1.41

author: NivagSwerdna
author_rank: Super Contributor
timestamp: May 18, 2017, 09:57:10 pm
content: Quote from: cv007 on May 18, 2017, 09:34:01 pmJust tried v1.42 and the 752 warnings are gone.... but now I get lots of...main.c:140: warning: (373) implicit signed to unsigned conversion#define UP_LED LATAbits.LA7static bit shortFlasher;...UP_LED = shortFlasher;

author: cv007
author_rank: Frequent Contributor
timestamp: May 18, 2017, 10:54:57 pm
content: Tried it, no warnings for me- XC8 version 1.41LATAbits.LA7- shouldn't that be LATAbits.LATA7 ?

author: rcbuck
author_rank: Frequent Contributor
timestamp: May 18, 2017, 11:54:25 pm
content: MPLAB X v3.45XC8 (v1.42)This code:main(){signed char myThing;myThing = -1;while(1) // main loop{CLRWDT();while(myThing < 125){myThing++;}// more code below here} // end while(1)} // end mainproduces no warning.

author: NivagSwerdna
author_rank: Super Contributor
timestamp: May 18, 2017, 11:55:54 pm
content: Quote from: cv007 on May 18, 2017, 10:54:57 pmI'm not sure what the difference between LATAbits.LA7 and LATAbits.LATA7 is!??! They look equivalent in struct/bitfield terms?

author: NivagSwerdna
author_rank: Super Contributor
timestamp: May 19, 2017, 12:00:43 am
content: Quote from: rcbuck on May 18, 2017, 11:54:25 pmProduces 2 warnings with 1.36...main.c:67: warning: (371) missing basic type; int assumedmain.c:70: warning: (752) conversion to shorter data typeProduces 1 warning with 1.42...main.c:67: warning: (371) missing basic type; int assumedThe 371 warning seems very sensible.

author: cv007
author_rank: Frequent Contributor
timestamp: May 19, 2017, 12:56:39 am
content: QuoteThe header for the chip I was using defines all bits in the LATXbits struct as LATXN, not LXN, yours must be different- disregard.

author: BrianHG
author_rank: Super Contributor
timestamp: May 19, 2017, 01:28:55 am
content: I'm curious, I thought that chars & ints were by default signed, and if you didn't want signage, you would use the term unsigned.Have I been doing thing backwards in XC8?Or, have I just been lucky that my code always worked using negative numbers just using ints and chars?

author: rcbuck
author_rank: Frequent Contributor
timestamp: May 19, 2017, 02:56:47 am
content: QuoteSorry, my main() is actually void main(void) - I just manually typed main in the reply.QuoteFrom the XC8 user's manual page 143:The MPLAB XC8 compiler supports integer data types with 1, 2, 3 and 4 byte sizes as well as a single bit type. Table 5-1 shows the data types and their corresponding size and arithmetic type. The default type for each type is underlined.TABLE 5-1: INTEGER DATA TYPESType Size (bits) Arithmetic Typeunsigned char8 Unsigned integer

author: BrianHG
author_rank: Super Contributor
timestamp: May 19, 2017, 04:44:56 am
content: Quote from: rcbuck on May 19, 2017, 02:56:47 amSee attached image:After reading the finer text 3 paragraphs after the table, signed is the default except for 'char', so, you are correct, though there is no underlining...

author: cv007
author_rank: Frequent Contributor
timestamp: May 19, 2017, 04:20:21 pm
content: That's why stdint.h was invented, so you can just tell the compiler what size you want and whether its signed or not- no table lookup required. When you use a uint16_t, int8_t, uint8_t - its obvious to you, the compiler, and anybody reading your code what that means.

author: rcbuck
author_rank: Frequent Contributor
timestamp: May 19, 2017, 09:16:10 pm
content: QuoteNo underlining must be your PDF viewer. The screen shot seems to indicate you are running Linux. I see the underlining with Adobe Acrobat.As you say, the 3rd paragraph down does clarify the data types.As cv007 suggests, I always use uint8_t, etc... as it makes it clear. I wonder if Microchip is defaulting char to unsigned as a carry over from the ASCII table?

author: BrianHG
author_rank: Super Contributor
timestamp: May 19, 2017, 09:20:51 pm
content: Quote from: rcbuck on May 19, 2017, 09:16:10 pmSorry, but I am running windows 7.And I am using Adobe Acrobat Reader version 2017.009.20044, Copyright 1984-2017 Adobe Systems Incorporated.

author: rcbuck
author_rank: Frequent Contributor
timestamp: May 19, 2017, 10:57:51 pm
content: Brian, I was using the XC8 manual from 2012. I just downloaded the XC8 2012-2016 version. I do not see the underlining in that version even though they state default types are underlined. You gotta love Microchip.I am running the same version Acrobat that you are. However, I am using Windows 10, which sucks compared to Windows 7.