Comparing Char Against Integer Literal

Z

Zach

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
 
R

Rob Williscroft

Zach wrote in 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.
 
A

Andrew Koenig

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.
 
M

Mike Wahler

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

#include <iostream>

#include said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top