473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

comparisons errors

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
Nov 27 '07 #1
5 2368
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".
Nov 27 '07 #2
<hy*******@gmail.comwrote in message
news:f3**********************************@e4g2000h sg.googlegroups.com...
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


Nov 27 '07 #3
hy*******@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
Nov 27 '07 #4
Thanks you for the replies everyone!! It was certainly enlightening!

Hua-Ying
Nov 27 '07 #5
On Tue, 27 Nov 2007 21:03:53 +0200, Eero Harmaala
<ee***********@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.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Nov 28 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: A.M. Kuchling | last post by:
python.org has a page of "Python vs. X" language comparisons at <http://www.python.org/doc/Comparisons.html>. They're all pretty outdated, and often unfair because they're written by a person who...
0
by: Mike Rovner | last post by:
Mike Rovner wrote: > A.M. Kuchling wrote: >> python.org has a page of "Python vs. X" language comparisons at >> <http://www.python.org/doc/Comparisons.html>. They're all pretty >> outdated, and...
11
by: Dietrich Epp | last post by:
Without invoking double-underscore black magic, it is possible to choose a, b, and c in Python such that: a < b b < c c < a This could cause sorting functions to malfunction. >>> class...
5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
3
by: Kevin King | last post by:
I have a question about an assignment I have. I need to count the number of comparisons in my merge sort. I know that the function is roughly nlog(n), but I am definately coming up with too many...
10
by: Peter Ammon | last post by:
I'm fuzzy on exactly what I can depend on when doing floating point comparisons. I know that calculated values can differ, e.g. I'm not guaranteed that 1. + 2. == 3. What about comparisons with...
4
by: Aamir Mahmood | last post by:
Hi I am on framework version 1.1 (SP1). Consider the following code. public static void Main() { int i=1; int j=1;
1
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
21
by: Damian | last post by:
Hi, I'm from an ASP.NET background an am considering making the switch to Python. I decided to develop my next project in tandem to test the waters and everything is working well, loving the...
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.