comparisons errors

H

hyling.s1

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

char temp[12];

int temp2 = sizeof(char);
QCoreApplication 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
 
E

Eero Harmaala

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

Mike Wahler

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

char temp[12];

int temp2 = sizeof(char);
QCoreApplication 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
 
A

Andrey Tarasevich

...
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'.
 
J

Jack Klein

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.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 

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,048
Latest member
verona

Latest Threads

Top