Menu

Strange value range for 8 bit datatypes

2008-01-03
2012-09-26
  • piccioni mose

    piccioni mose - 2008-01-03

    Hi,
    I'm having some troubles with Dev-C++ Version 4.9.9.2 running on Windows XP Pro.
    It seems that an unsigned char variable can handle values from -128 to 255 and a signed char variable too.
    I try to include stdint.h and to use uint8_t and int8_t but I obtain the same result.
    Do you know something about?

     
    • Osito

      Osito - 2008-01-03

      When you say handle, what do you mean? You can certainly set an unsigned char to 255 or any other value, regardless of whether it's out of range. Setting an 8-bit char to 255 would make it -1 (I think).

       
    • piccioni mose

      piccioni mose - 2008-01-03

      Sorry, I don't explain my problem very well and I apologize for that.
      First of all unsigned char and signed char are certainly different: an unsigned char can assume value from 0 to 255 and not from -128 to 255, moreover a signed char can assume value from -128 to 127 and not from -128 to 255. When I assign a value out of the proper range (0;255 for unsigned char and -128;127 for signed char) the compiler doesn't produce an error. When the value is less than -128 and more than 255 the compiler produce "only" a warning message.
      You are right If a variable is a signed char the value 0xFF is represented in two-complement it's -1 but if the variable is unsigned 0xFF is 255.

       
      • Osito

        Osito - 2008-01-03

        I guess my point is, it's not an error to assign 255 to a signed char. Maybe you can set a warning flag in gcc to catch that, but by default it's an acceptable thing to do.

         
    • cpns

      cpns - 2008-01-03

      Beats me why you did not think to just post some code in the first place rather than waste time 'describing' code!

      The implicit type cast is valid. Have you tried setting -Werror -Wall

      See: http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Warning-Options.html#Warning-Options for other warning options.

      Clifford

       
    • piccioni mose

      piccioni mose - 2008-01-03

      I apologize for my poor English.
      You are completely right, if I want to assign -1 to a signed char variable I use a code like this:

      signed char a;
      a = -1;

      and not:

      a = 255;

      But my problem is that I can't explain why the compiler allows to assign value out of the "canonical" ranges. I looked in the various option and preferences menu but I can't find nothing to solve the problem.
      I've some experience with microcontrollers, when a wrong value (out of range) is assigned to a variable the compiler return an error without conmpiling the project.

       
    • Osito

      Osito - 2008-01-03

      But both of those are the same. The compiler won't complain at all.

      I think -Wconversion will make it complain about unsigned char a; a=-1;

      I'm not sure if there's a check for signed char a; a=255;

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.