-1

Shouldn't I receive an error message for doing this?

  unsigned char n = -500;
  //or this
  Byte n = -500;

This shouldn't even compile!

  • The compiler simply converts the value to fit in the allotted space. There is probably a warning that can be enabled to catch this. But be prepared for a flood of new warnings. – rmaddy Nov 19 '13 at 05:52
  • But that doesn't make sense. When I type 'unsigned' I clearly mean NO negative values. Right? – tiffanyButterfly23 Nov 19 '13 at 05:55
  • `char` stores a value from -128 to 127. `unsigned char` stores 0 to 255. The -500 is simply converted to fit in the range 0 to 255. Of course during this conversion its value is essentially turned into a useless value. But simply making a variable `unsigned` in no way prevents you from assigning negative values. – rmaddy Nov 19 '13 at 05:57
  • 2
    BTW - this is really a basic question from the C language. I'm sure some searching on the topic will turn up much better explanations. – rmaddy Nov 19 '13 at 05:59
  • 1
    See http://stackoverflow.com/questions/5169692/assigning-negative-numbers-to-an-unsigned-int – rmaddy Nov 19 '13 at 06:01

1 Answers1

4

Enable SIGN_COMPARE from Build Settings of your target and test.

enter image description here

βhargavḯ
  • 9,786
  • 1
  • 37
  • 59