Comparing Char Against Integer Literal

Discussion in 'C++' started by Zach, Dec 12, 2004.

  1. Zach

    Zach Guest

    What happens when you compare a char against an integer literal?
    For example,

    #include <iostream>
    using namespace std;
    int main(int argc, char* argv[]) {
    signed char c1 = 150;
    if (c1 < 150)
    cout << "Less than 150.\n";
    else
    cout << "Not less than 150.\n";
    if (static_cast<unsigned char>(c1) < 150)
    cout << "Less than 150.\n";
    else
    cout << "Not less than 150\n";
    return 0;
    }

    VC++6.0 outputs "Less than 150.\nNot less than 150\n". I understand why
    this would happen, but my question is, is this right? This disturbs me
    because sometimes I want to read bytes in from a file and I'd like to
    think that I could compare them against a hex literal. What should I
    do?

    Zach
    Zach, Dec 12, 2004
    #1
    1. Advertising

  2. Zach wrote in news:
    in comp.lang.c++:

    > What happens when you compare a char against an integer literal?
    > For example,
    >
    > #include <iostream>
    > using namespace std;
    > int main(int argc, char* argv[]) {
    > signed char c1 = 150;
    > if (c1 < 150)
    > cout << "Less than 150.\n";
    > else
    > cout << "Not less than 150.\n";
    > if (static_cast<unsigned char>(c1) < 150)
    > cout << "Less than 150.\n";
    > else
    > cout << "Not less than 150\n";
    > return 0;
    > }
    >
    > VC++6.0 outputs "Less than 150.\nNot less than 150\n". I understand why
    > this would happen, but my question is, is this right?



    Yep, the type of a literal like 150 is int so the compiler will convert
    (widen) the signed or unsigned char to int before doing the comparison.

    Note that the error (if you consider it such) is in assigning 150 to
    a type (signed char) that can't represent the value.

    > This disturbs me
    > because sometimes I want to read bytes in from a file and I'd like to
    > think that I could compare them against a hex literal. What should I
    > do?


    When ever you want to read "bytes" use unsigned char (or arrays or
    std::vector<>s of) to store the "bytes".

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Dec 12, 2004
    #2
    1. Advertising

  3. "Zach" <> wrote in message
    news:...

    > What happens when you compare a char against an integer literal?
    > For example,
    >
    > #include <iostream>
    > using namespace std;
    > int main(int argc, char* argv[]) {
    > signed char c1 = 150;


    We can stop right here. On a machine with 8-bit chars, 150 does not fit in
    a signed char.
    The effect of assigning a value to a signed object that can't hold it is
    undefined, so this program produces undefined behavior. Anything that might
    happen after this point is irrelevant.
    Andrew Koenig, Dec 12, 2004
    #3
  4. Zach

    Mike Wahler Guest

    "Zach" <> wrote in message
    news:...
    > What happens when you compare a char against an integer literal?
    > For example,
    >
    > #include <iostream>


    #include <limits>

    > using namespace std;
    > int main(int argc, char* argv[]) {
    > signed char c1 = 150;


    signed char c1(0);
    int value(150);
    signed char mx(std::numeric_limits<signed char>::max();

    cout << "maximum possible value for type"
    "'signed char' for this implementation is "
    << mx << '\n';

    if(value <= mx)
    c1 = value;
    else
    {
    cout << "Value of " << value << " is outside the "
    "allowed range for this implementation\n";
    c1 = mx; /* or whatever 'default' you find appropriate */
    }

    > if (c1 < 150)
    > cout << "Less than 150.\n";
    > else
    > cout << "Not less than 150.\n";
    > if (static_cast<unsigned char>(c1) < 150)
    > cout << "Less than 150.\n";
    > else
    > cout << "Not less than 150\n";
    > return 0;
    > }
    >
    > VC++6.0 outputs "Less than 150.\nNot less than 150\n". I understand why
    > this would happen,


    Are you sure? According to the standard, anything could happen
    (your code's behavior is undefined).

    >but my question is, is this right?


    Since the behavior is undefined, any result would be considered 'right'.

    >This disturbs me
    > because sometimes I want to read bytes in from a file and I'd like to
    > think that I could compare them against a hex literal. What should I
    > do?


    If you want to read 'raw' bytes, always use type 'unsigned char'.


    -Mike
    Mike Wahler, Dec 12, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lovecreatesbeauty
    Replies:
    1
    Views:
    987
    Ian Collins
    May 9, 2006
  2. , India

    Comparing char * with string literal

    , India, Mar 8, 2007, in forum: C Programming
    Replies:
    9
    Views:
    496
    Malcolm McLean
    Mar 8, 2007
  3. william
    Replies:
    18
    Views:
    530
    CBFalconer
    Mar 19, 2007
  4. william
    Replies:
    10
    Views:
    748
    Gavin Deane
    Mar 19, 2007
  5. Replies:
    5
    Views:
    211
    Tassilo v. Parseval
    Nov 22, 2005
Loading...

Share This Page