PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > What could the compiler be possibly complaining about
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
What could the compiler be possibly complaining about

Réponse
 
LinkBack Outils de la discussion
Vieux 06/03/2008, 11h51   #1
parag_paul@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut What could the compiler be possibly complaining about

Are there any compiler groups.
Though I try it in this language forum
I got the following message for the following line in a c file

if (logic_value_change == p_variable_info->vc_reason &&
p_variable_info->p_value->logic_value <4 &&
p_variable_info->p_value->logic_value>=0)



The message is
.../star186559.c: In function `func_var':
.../star186559.c:320: warning: comparison is always true due to limited
range of data type



comparison is always true ????

The data type fpr logic_value is a unsigned char
-Parag
  Réponse avec citation
Vieux 06/03/2008, 12h03   #2
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What could the compiler be possibly complaining about

parag_paul@hotmail.com said:

> Are there any compiler groups.


Yes, but you don't need one for this question, which is about the C
language.

> Though I try it in this language forum
> I got the following message for the following line in a c file
>
> if (logic_value_change == p_variable_info->vc_reason &&
> p_variable_info->p_value->logic_value <4 &&
> p_variable_info->p_value->logic_value>=0)
>
> The message is
> ../star186559.c: In function `func_var':
> ../star186559.c:320: warning: comparison is always true due to limited
> range of data type
>
> comparison is always true ????


Yes.

> The data type fpr logic_value is a unsigned char


An unsigned char cannot hold negative values. It can only hold values in
the range 0 to UCHAR_MAX, which is guaranteed to be a positive integer
value that is at least 255 (but is higher on some systems).

The comparison you have is: p_variable_info->p_value->logic_value>=0

Now, *any* unsigned char value *must* have a value greater than or equal to
0, by definition. Therefore, the comparison >= 0 will always yield true
for an unsigned char, which is exactly what the compiler is (correctly)
telling you.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  Réponse avec citation
Vieux 06/03/2008, 18h22   #3
Martin Ambuhl
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What could the compiler be possibly complaining about

parag_paul@hotmail.com wrote:
> Are there any compiler groups.
> Though I try it in this language forum
> I got the following message for the following line in a c file
>
> if (logic_value_change == p_variable_info->vc_reason &&
> p_variable_info->p_value->logic_value <4 &&
> p_variable_info->p_value->logic_value>=0)


> The message is
> .../star186559.c: In function `func_var':
> .../star186559.c:320: warning: comparison is always true due to limited
> range of data type


> comparison is always true ????


> The data type fpr logic_value is a unsigned char


The comparison
p_variable_info->p_value->logic_value>=0)
is obviously always true if logic_value is an unsigned char.
An unsigned char cannot be less than zero.
  Réponse avec citation
Vieux 07/03/2008, 04h13   #4
Falcon Kirtaran
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What could the compiler be possibly complaining about

parag_paul@hotmail.com wrote:
> Are there any compiler groups.
> Though I try it in this language forum
> I got the following message for the following line in a c file
>
> if (logic_value_change == p_variable_info->vc_reason &&
> p_variable_info->p_value->logic_value <4 &&
> p_variable_info->p_value->logic_value>=0)
>
>
>
> The message is
> ../star186559.c: In function `func_var':
> ../star186559.c:320: warning: comparison is always true due to limited
> range of data type
>
>
>
> comparison is always true ????
>
> The data type for logic_value is a unsigned char
> -Parag


That warning sometimes comes up when you compare variables of different
sizes. To really know what you need to do differently, I'd need the
types of all the other variables you are comparing as well.

To illustrate what is happening here, consider an unsigned char.
Because it is only eight bytes long and can store values no larger than
255, if you write something like:

int function {
char a = 255;
if (a < 256) return 1;
};

The comparison will always be true and the function will always return 1.

As a point of practice, it's always a good idea to put parentheses
around the parts of a condition, eg:

if ( (logic_value_change == p_variable_info->vc_reason) &&
(p_variable_info->p_value->logic_value < 4) &&
(p_variable_info->p_value->logic_value >= 0) )

However, conveniently, the precedence of ==, <, and >= are all greater
than that of &&, so that shouldn't be your issue. -> trumps all of
those. I'm guessing that the issue is as simple as logic_value_change
and vc_reason having silly types.

--
--Falcon Kirtaran
  Réponse avec citation
Vieux 09/03/2008, 22h44   #5
Falcon Kirtaran
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What could the compiler be possibly complaining about

Falcon Kirtaran wrote:
> parag_paul@hotmail.com wrote:
>> Are there any compiler groups.
>> Though I try it in this language forum
>> I got the following message for the following line in a c file
>>
>> if (logic_value_change == p_variable_info->vc_reason &&
>> p_variable_info->p_value->logic_value <4 &&
>> p_variable_info->p_value->logic_value>=0)
>>
>>
>>
>> The message is
>> ../star186559.c: In function `func_var':
>> ../star186559.c:320: warning: comparison is always true due to limited
>> range of data type
>>
>>
>>
>> comparison is always true ????
>>
>> The data type for logic_value is a unsigned char
>> -Parag

>
> That warning sometimes comes up when you compare variables of different
> sizes. To really know what you need to do differently, I'd need the
> types of all the other variables you are comparing as well.
>
> To illustrate what is happening here, consider an unsigned char. Because
> it is only eight bytes long and can store values no larger than 255, if
> you write something like:
>
> int function {
> char a = 255;
> if (a < 256) return 1;
> };
>
> The comparison will always be true and the function will always return 1.
>
> As a point of practice, it's always a good idea to put parentheses
> around the parts of a condition, eg:
>
> if ( (logic_value_change == p_variable_info->vc_reason) &&
> (p_variable_info->p_value->logic_value < 4) &&
> (p_variable_info->p_value->logic_value >= 0) )
>
> However, conveniently, the precedence of ==, <, and >= are all greater
> than that of &&, so that shouldn't be your issue. -> trumps all of
> those. I'm guessing that the issue is as simple as logic_value_change
> and vc_reason having silly types.
>
> --
> --Falcon Kirtaran


Bah. Clearly, I am a fool. Your problem is "logic_value>=0" because
unsigned types can't contain negative numbers.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h49.


Édité par : vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14326 seconds with 13 queries