Need to forceful sign my types all of a sudden?

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Using avrstudio 6 and an atmega 32u4 chip

 

using a wire for debug. I see the 10ms and then the 13ms.

    //initialize the BB and controller connected.
DDRD |= 0x80;PORTD &= ~0x80;_delay_ms(10);PORTD |= 0x80;DDRD &= ~0x80;
    char init = -1;
    while ( init == -1)
    {    
DDRD |= 0x80;PORTD &= ~0x80;_delay_ms(11);PORTD |= 0x80;DDRD &= ~0x80;
    
        bridgeInterface->write(BRIDGE_CONTROLLER_POLL);
        bridgeInterface->write(BRIDGE_GET_CONTROLLER_STATUS);
        init =bridgeInterface->read(13);

    }
DDRD |= 0x80;PORTD &= ~0x80;_delay_ms(13);PORTD |= 0x80;DDRD &= ~0x80;

    It seems the char is forced to be unsigned. I never had to forcefully sign my types before. I put a signed in there and it works. My question is why all of a sudden or only with a 32u4? This is the same compile I use with my avr 328 and the default char is singed.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Handling sign is a function of the language and its compiler, not the hardware. In C, I believe that char is inherently unsigned.

 

IMHO, one should never use type char for a value that can be negative. In fact, char type is really designed for ASCII characters which are always positive integers from 0 to 127. So, if you ae using it for something other than a character,  type it int8_t and get correct results.

 

Jim

 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sat. Sep 21, 2019 - 03:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Good point I foolishly always use char because the damn int8_t syntax highly does not show up. I wish AVR studio would show that in purple like char.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One of the quirks of C is that plain char can be signed or unsigned (ie. it's implementation defined).

If you're using char specifically for 'small numbers' as opposed to 'characters' then either you should always be explicit and use 'signed char' or 'unsigned char', or use int8_t and uint8_t.

(if needs be you can specify an option to the compiler to force it to be the one you want, but better to be explicit in the code).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Only use "char" for characters. For anything numeric use int8_t or uint8_t. 

 

(Same for wider types, never use short, int or long)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

At least part of the reason for Cliff's recommendation is that short, int, and such depend on the the device that the program is compiled for. On the other-hand, uint8_t is ALWAYS 8-bits wide, no matter the data width of the processor.

 

Now, I grant you that if your universe is ALWAYS an 8-bit AVR, this might not seem such a big deal. But, with the steady price decline for ARM chips, you may be using a 32-bit chip sooner than you think. Thus, it is a good practice to get used to and follow.

 

I do find the lack of syntax highlighting annoying, but have learned to live with it.

 

Jim

 

 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sat. Sep 21, 2019 - 08:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For avr-gcc, it's a compiler option, with an AS7 checkbox to control it in the project configuration under "Toolchain/AVRGnu C Compiler/General"  ("Default char type is unsigned")

I think that it's one of those options that AS turns on by default (and I'm not entirely sure why.  I figure, the fewer compiler-specific options you need, the better.)