Re: When are unnecessary casts useful?
- From: Simon Biber <news@xxxxxxxxx>
- Date: Thu, 11 Jan 2007 12:36:08 +1100
David T. Ashley wrote:
"ais523" <ais523@xxxxxxxxxx> wrote in message news:1168424781.385448.300330@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[..]%cat -n test.c
1 int main(void)
2 {
3 char c='c';
4 char* a;
5 a=&c;
6 if(!a || *a > 10) c++;
7 if((!a) || (*a > 10)) c++;
8 if(*a = 10) c++;
9 if((*a = 10)) c++;
10 return 0;
11 }
%gcc -W -Wall test.c
test.c: In function `main':
test.c:8: warning: suggest parentheses around assignment used as truth
value
Note that my version of gcc hasn't warned on line 9; it takes the extra
parentheses as an instruction to suppress the warning. So excess
parenthesisation of this sort can have an effect on compiler
diagnostics, even if it doesn't affect the behaviour of the code.
But that the extra parens suppress it really surprises me.
The text of the diagnostic message clearly suggests adding brackets to assignments used as truth values. There must be a reason for doing so -- perhaps as mundane as simply to suppress the warning.
Maybe the rationale was if you parenthesize it you are saying "Yo, compiler, I do understand that this assignment has a value, and I want the value of it".
Yes, exactly.
Or (although I doubt it), it could be bug in the compiler.
No. It's by design.
Unfortunately not all compilers take the hint when you just add brackets -- some will continue to warn you. So I've taken to adding both brackets and != 0 to the end:
if((x = y) != 0)
This is pretty warning-proof.
--
Simon.
.
- Follow-Ups:
- Re: When are unnecessary casts useful?
- From: David T. Ashley
- Re: When are unnecessary casts useful?
- References:
- When are unnecessary casts useful?
- From: jacob navia
- Re: When are unnecessary casts useful?
- From: David T. Ashley
- Re: When are unnecessary casts useful?
- From: ais523
- Re: When are unnecessary casts useful?
- From: David T. Ashley
- When are unnecessary casts useful?
- Prev by Date: Re: Pausing/Waiting in C
- Next by Date: Re: Pausing/Waiting in C
- Previous by thread: Re: When are unnecessary casts useful?
- Next by thread: Re: When are unnecessary casts useful?
- Index(es):
Relevant Pages
|