comparisons errors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hyling.s1@gmail.com

    #1

    comparisons errors

    Would someone please enlighten me as to why each version behaves the
    way it does?

    char temp[12];

    int temp2 = sizeof(char);
    QCoreApplicatio n a(argc, argv);

    temp[0] = 0xFF;
    temp[1] = 0xEE;

    if(temp[0] == 0xFF) // returns false but if(temp[0] == (char)0xFF) //
    returns true

    {
    printf("yes");
    }
    else
    {
    printf("no");
    }

    Thanks
    Hua-Ying
  • Eero Harmaala

    #2
    Re: comparisons errors

    The range of char is simply just [-128, 127] so using
    if(temp[0] == 0xFF)
    returns always false as 0xFF = 255 is not in the range of char.

    My version of gcc gives the warning "[Warning] comparison is always
    false due to limited range of data type".

    Comment

    • Mike Wahler

      #3
      Re: comparisons errors

      <hyling.s1@gmai l.comwrote in message
      news:f3f577c8-1956-4325-837a-b9a963d48878@e4 g2000hsg.google groups.com...
      Would someone please enlighten me as to why each version behaves the
      way it does?
      >
      char temp[12];
      >
      int temp2 = sizeof(char);
      QCoreApplicatio n a(argc, argv);
      >
      temp[0] = 0xFF;
      temp[1] = 0xEE;
      >
      if(temp[0] == 0xFF) // returns false but if(temp[0] == (char)0xFF) //
      returns true
      >
      {
      printf("yes");
      }
      else
      {
      printf("no");
      }
      >
      Thanks
      Hua-Ying
      If type 'char' on your implementation is signed, it isn't
      guaranteed to be able to represent 0xFF. If your CHAR_BIT
      is 8, and type 'char' is signed, then 0xFF will probably be
      interpreted as -1, not 255.

      With MSVC++, whose size 'char' is signed and has eight bits:

      #include <iostream>

      int main()
      {
      int i = 0xFF;
      char c = 0xFF;
      std::cout << "i == " << i << '\n'
      << "c == " << int(c) << '\n';

      return 0;
      }

      Output:

      i == 255
      c == -1

      Try using unsigned char instead of char.

      -Mike




      Comment

      • Andrey Tarasevich

        #4
        Re: comparisons errors

        hyling.s1@gmail .com wrote:
        ...
        temp[0] = 0xFF;
        temp[1] = 0xEE;
        >
        if(temp[0] == 0xFF) // returns false but if(temp[0] == (char)0xFF) //
        returns true
        ...
        Let's get rid of the variables and convert everything to decimals. The
        above comparison is equivalent to

        if ((char) 255 == 255)

        Before the comparison takes place, the language will apply arithmetic
        promotions to both operands and promote them to type 'int' (assuming
        that in your compiler the range of 'char' fits into the range of 'int').
        The actual comparison will be carried out for 'int' operands. This means
        that what you wrote above is equivalent to

        if ((int)(char) 255 == (int) 255)

        Now, let's assume that in your compiler the type 'char' is signed and
        it's range is too small to accommodate the value 255. In that case the
        result of '(char) 255 ' conversion is implementation-defined. Most
        likely it is going to be some negative value, say, '-1'. Under that
        assumption, the above comparison is equivalent to

        if (-1 == 255)

        and, obviously, the result of this comparison is 'false'.

        Once you add the extra 'char' cast on the right-hand side, you end up with

        if ((int)(char) 255 == (int)(char) 255)

        which is going to be equivalent to

        if (-1 == -1)

        for the very same reasons (under the very same assumptions). This is, of
        course, 'true'.

        --
        Best regards,
        Andrey Tarasevich

        Comment

        • Hua-Ying

          #5
          Re: comparisons errors

          Thanks you for the replies everyone!! It was certainly enlightening!

          Hua-Ying

          Comment

          • Jack Klein

            #6
            Re: comparisons errors

            On Tue, 27 Nov 2007 21:03:53 +0200, Eero Harmaala
            <eero.harmaala@ utu.fiwrote in comp.lang.c++:
            The range of char is simply just [-128, 127] so using
            if(temp[0] == 0xFF)
            returns always false as 0xFF = 255 is not in the range of char.
            You mean the range of char ***in your particular implementation* **.
            The plain char type may be either signed or unsigned. It must be able
            to contain at least a range of either -127 (not -128) to 127, or 0 to
            255. It may contain a far wider range.
            My version of gcc gives the warning "[Warning] comparison is always
            false due to limited range of data type".
            Try a typical compiler for an ARM processor, just for example, or a
            DSP with 16 or 32 bit characters, and this warning won't appear.

            --
            Jack Klein
            Home: http://JK-Technology.Com
            FAQs for
            comp.lang.c http://c-faq.com/
            comp.lang.c++ http://www.parashift.com/c++-faq-lite/
            alt.comp.lang.l earn.c-c++

            Comment

            Working...