warning: comparison is always false due to limited range of data type
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
You are trying to take the values of 'TRUE' and 'FALSE' and return a single char... the literals 'TRUE' and 'FALSE' actually represent a 4 byte integer, not a single char. You cannot store 4 bytes of data into a 1 byte char.
Try changing it to return a single char, or just return 0 or 1...
Edit: Actually there would be 5 bytes in 'FALSE' so that actually even be too long for a 4 byte int... testing the simple program below with g++ I get the following:
I think you got the first warning, because multicharacter character constants are seldom used in my experience, so the compiler was warning you in case you didn't really want to do that. The second warning was because FALSE is 5 bytes, so it couldn't be properly stored in a 4 byte int.
Note: 'TRUE' works out to the value 0x54525545, because the letters have the following ASCII values:
T = 0x54
R = 0x52
U = 0x55
E = 0x45
If you take a look at Hko's sample code, he shows you the more common way to create TRUE and FALSE constants...
Not to beat a dead horse, but I wanted to say something that nobody else has addressed yet.
In C/C++ there is a difference between using single quotes ( ' ) and double quotes ( " ). Using single quotes implies a character literal. More specifically, a char variable. Using double quotes implies a string (multi-char sequence terminated by a NULL).
So, typing 'TRUE' or 'FALSE' doesn't make any sense, and I would have expected the compiler to complain... loudly (as in a fatal error). You could have used 'T' and 'F' however.
Last edited by Dark_Helmet; 12-20-2004 at 05:05 PM.
Actually, 'TRUE' in both C and C++ is prefectly valid. It just doesn't have quite the same meaning as a typical char literal. As I stated before, this is called a multicharacter character constant. It is seldom used, which is why the compiler probably issues a warning. (The fact that few people know about it is further evidence that it is very seldom used. I thought it must be wrong the first time I ever saw it too...)
As I stated before, when using a multicharacter character constant like that, it basically converts to a 4 byte integer. I put the actual values of what 'TRUE' evaluates to above, but if you want to test yourself, try the following:
I could be wrong, but I believe that the idea of multicharacter character constants originated with the usage of wide character sets, which generally use 2 bytes per character...
Trying to search for some more info on them, but there doesn't even seem to be much on Google. If I find anything of interest I'll post it here.
I didn't find much that was conclusive information on this, but apparently, the ANSI C specification states that the behavior of multicharacter constants is implementation specific. Here is a Usenet thread that quotes a few documents on the topic:
Found another link that talks a bit about the GNU implementation of multi-character character constants here.
Here's a quote from part of it:
The compiler values a multi-character character constant a character at a time, shifting the previous value left by the number of bits per target character, and then or-ing in the bit-pattern of the new character truncated to the width of a target character. The final bit-pattern is given type int, and is therefore signed, regardless of whether single characters are signed or not (a slight change from versions 3.1 and earlier of GCC). If there are more characters in the constant than would fit in the target int the compiler issues a warning, and the excess leading characters are ignored.
I know what you mean bigearsbilly. Just when you think you know everything about C/C++, you learn some new obscure thing like this. I'm not sure why, but obscure stuff like this interests me and usually sticks in my head...
BTW, I am probably wrong about the multicharacter constants originating for wide character sets. Literals for those are generally signified something like: wchar_t ch = L'a';. It sounds like the multicharacter constant like 'abcd' is specifically for providing a different way to give a literal integer value.
I think the first place I ever saw this convention used was in some sample code for writing apps for PalmOS using their modified gcc compiler... I was like, WTF!?!