Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > parentheses question

Reply
Thread Tools

parentheses question

 
 
Zach
Guest
Posts: n/a
 
      11-19-2010
I don't understand this warning. From what I've read in K&R this is
valid C:

ll14.c: In function 'process_tokens':
ll14.c:156: warning: suggest parentheses around '&&' within '||'

Relevant code:

if ((((c[0] == 'F' || 'R' || 'O' || 'K')
&& (isdigit(c[1]) || islower(c[1])))
&& (c[2] == '-')
&& (c[3] == '>'))
&& ((c[4] == 'F' || 'R' || 'O' || 'K')
&& (isdigit(c[5]) || islower(c[5]))
|| ((c[4] == 'A') && (c[5] == 'L') && (c[6] == 'L'))
|| ((c[4] == 'F') && (c[5] == 'E') && (c[6] == 'D'))
|| ((c[4] == 'R') && (c[5] == 'O') && (c[6] == 'M'))
|| ((c[4] == 'O') && (c[5] == 'R') && (c[6] == 'I'))
|| ((c[4] == 'K') && (c[5] == 'L') && (c[6] == 'I'))))

Line 156 is:
|| ((c[4] == 'F') && (c[5] == 'E') && (c[6] == 'D'))

Also I don't understand why it complains only about that line and not
the others. Weird.

Zach
 
Reply With Quote
 
 
 
 
Zach
Guest
Posts: n/a
 
      11-19-2010
On Nov 19, 7:08*am, pete <(E-Mail Removed)> wrote:
> Zach wrote:
>
> > I don't understand this warning. From what I've read in K&R this is
> > valid C:

>
> > ll14.c: In function 'process_tokens':
> > ll14.c:156: warning: suggest parentheses around '&&' within '||'

>
> > Relevant code:

>
> > * if ((((c[0] == 'F' || 'R' || 'O' || 'K')
> > * * * * && (isdigit(c[1]) || islower(c[1])))
> > * * * *&& (c[2] == '-')
> > * * * *&& (c[3] == '>'))
> > * * * && ((c[4] == 'F' || 'R' || 'O' || 'K')
> > * * * * * && (isdigit(c[5]) || islower(c[5]))
> > * * * || ((c[4] == 'A') && (c[5] == 'L') && (c[6] == 'L'))
> > * * * || ((c[4] == 'F') && (c[5] == 'E') && (c[6] == 'D'))
> > * * * || ((c[4] == 'R') && (c[5] == 'O') && (c[6] == 'M'))
> > * * * || ((c[4] == 'O') && (c[5] == 'R') && (c[6] == 'I'))
> > * * * * * || ((c[4] == 'K') && (c[5] == 'L') && (c[6] == 'I'))))

>
> > Line 156 is:
> > * * * || ((c[4] == 'F') && (c[5] == 'E') && (c[6] == 'D'))

>
> > Also I don't understand why it complains only about that line and not
> > the others. Weird.

>
> Regardless of the value of (X),
> (X == 'F' || 'R' || 'O' || 'K') is always equal to (1).


That has nothing to do with the warning. Thanks but I already knew
that. The code works fine, the problem is why that warning is
generated. And why it is generated for only that 1 line.

Zach
 
Reply With Quote
 
 
 
 
Zach
Guest
Posts: n/a
 
      11-19-2010
On Nov 19, 7:22*am, pete <(E-Mail Removed)> wrote:
> Zach wrote:
>
> > On Nov 19, 7:08 am, pete <(E-Mail Removed)> wrote:
> > > Zach wrote:

>
> > > > I don't understand this warning. From what I've read in K&R this is
> > > > valid C:

>
> > > > ll14.c: In function 'process_tokens':
> > > > ll14.c:156: warning: suggest parentheses around '&&' within '||'

>
> > > > Relevant code:

>
> > > > * if ((((c[0] == 'F' || 'R' || 'O' || 'K')
> > > > * * * * && (isdigit(c[1]) || islower(c[1])))
> > > > * * * *&& (c[2] == '-')
> > > > * * * *&& (c[3] == '>'))
> > > > * * * && ((c[4] == 'F' || 'R' || 'O' || 'K')
> > > > * * * * * && (isdigit(c[5]) || islower(c[5]))
> > > > * * * || ((c[4] == 'A') && (c[5] == 'L') && (c[6] == 'L'))
> > > > * * * || ((c[4] == 'F') && (c[5] == 'E') && (c[6] == 'D'))
> > > > * * * || ((c[4] == 'R') && (c[5] == 'O') && (c[6] == 'M'))
> > > > * * * || ((c[4] == 'O') && (c[5] == 'R') && (c[6] == 'I'))
> > > > * * * * * || ((c[4] == 'K') && (c[5] == 'L') && (c[6] == 'I'))))

>
> > > > Line 156 is:
> > > > * * * || ((c[4] == 'F') && (c[5] == 'E') && (c[6] == 'D'))

>
> > > > Also I don't understand why it complains only about that line and not
> > > > the others. Weird.

>
> > > Regardless of the value of (X),
> > > (X == 'F' || 'R' || 'O' || 'K') is always equal to (1).

>
> > That has nothing to do with the warning.

>
> Don't forget that your premise
> is that you don't know what is causing the warning.
>
> > Thanks but I already knew
> > that. The code works fine, the problem is why that warning is
> > generated. And why it is generated for only that 1 line.

>
> When I removed those expressions and their associated (&&) operators,
> I got a program which runs and compiles without warnings.
>
> /* BEGIN new.c */
>
> #include <stdio.h>
> #include <ctype.h>
>
> int main(void)
> {
> * * char c[] = "123456789";
>
> * * if ((((isdigit(c[1]) || islower(c[1])))
> * * * *&& (c[2] == '-')
> * * * *&& (c[3] == '>'))
> * * * && ((isdigit(c[5]) || islower(c[5]))
> * * * || ((c[4] == 'A') && (c[5] == 'L') && (c[6] == 'L'))
> * * * || ((c[4] == 'F') && (c[5] == 'E') && (c[6] == 'D'))
> * * * || ((c[4] == 'R') && (c[5] == 'O') && (c[6] == 'M'))
> * * * || ((c[4] == 'O') && (c[5] == 'R') && (c[6] == 'I'))
> * * * * * || ((c[4] == 'K') && (c[5] == 'L') && (c[6] == 'I'))))
> * * {
> * * * * puts(c + 1);
> * * } else {
> * * * * puts(c);
> * * }
> * * return 0;
>
> }
>
> /* END new.c */
>
> --
> pete


Hi Pete,

Yes, but I needed the other code to test for whether I wanted that
token. For example a log file with line:

10:10:10 F2->R4 hello

I need this code to to test the "F2->R4" token:

((c[0] == 'F' || 'R' || 'O' || 'K')
&& (isdigit(c[1]) || islower(c[1])))

I only want to further process lines for which this is true.

Zach
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      11-19-2010
Zach wrote:
) 10:10:10 F2->R4 hello
)
) I need this code to to test the "F2->R4" token:
)
) ((c[0] == 'F' || 'R' || 'O' || 'K')
) && (isdigit(c[1]) || islower(c[1])))
)
) I only want to further process lines for which this is true.

That test will also be true for this line:
10:10:10 X2->R4 hello


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Zach
Guest
Posts: n/a
 
      11-19-2010
On Nov 19, 8:20*am, Willem <(E-Mail Removed)> wrote:
>
> That test will also be true for this line:
> 10:10:10 X2->R4 hello


Why? X is not specified as a conditional in the code. It should only
match for 'F','R','O','K'.

Zach
 
Reply With Quote
 
Zach
Guest
Posts: n/a
 
      11-19-2010
On Nov 19, 8:20*am, Willem <(E-Mail Removed)> wrote:
>
> That test will also be true for this line:
> 10:10:10 X2->R4 hello


I just did a test and you're correct. How can I have it be true ONLY
IF c[0] matches either 'F','R','O','K'?

Zach
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      11-19-2010
In article <(E-Mail Removed)>,
Zach <(E-Mail Removed)> wrote:
>On Nov 19, 8:20*am, Willem <(E-Mail Removed)> wrote:
>>
>> That test will also be true for this line:
>> 10:10:10 X2->R4 hello

>
>Why? X is not specified as a conditional in the code. It should only
>match for 'F','R','O','K'.
>
>Zach


People are dancing around telling you directly what's wrong, presumably
because they don't want to risk hurting your feelings.

But the point is that when you write:

(c[0] == 'F' || 'R' || 'O' || 'K')

You are checking to see if any of the following conditions are true:

c[0] == 'F'
'R'
'O'
'K'

Now, the first one in my list might be true or might not be (depending
on the value of c[0]). But the rest of them are all true regardless of
anything, since they are constants.

You probably meant:

(c[0] == 'F' ||
c[0] == 'R' ||
c[0] == 'O' ||
c[0] == 'K')

--
They say compassion is a virtue, but I don't have the time!

- David Byrne -

 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      11-19-2010


"Zach" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Nov 19, 8:20 am, Willem <(E-Mail Removed)> wrote:
>>
>> That test will also be true for this line:
>> 10:10:10 X2->R4 hello

>
> I just did a test and you're correct. How can I have it be true ONLY
> IF c[0] matches either 'F','R','O','K'?


(c[0] == 'F' ||c[0]== 'R' ||c[0]== 'O' ||c[0]== 'K')

--
Bartc


 
Reply With Quote
 
Zach
Guest
Posts: n/a
 
      11-19-2010
On Nov 19, 8:37*am, (E-Mail Removed) (Kenny McCormack)
wrote:
>
> People are dancing around telling you directly what's wrong, presumably
> because they don't want to risk hurting your feelings.


Hi Kenny,

If I was worried about hurting a frail ego and was psychologically so
insecure I wouldn't have even bothered posting.

> Now, the first one in my list might be true or might not be (depending
> on the value of c[0]). *But the rest of them are all true regardless of
> anything, since they are constants.
>
> You probably meant:
>
> (c[0] == 'F' ||
> *c[0] == 'R' ||
> *c[0] == 'O' ||
> *c[0] == 'K')


Yes, my mistake was thinking that the way I wrote it was equivalent to
this. Lesson learned. BTW I still get the warning:

ll15.c: In function 'process_tokens':
ll15.c:156: warning: suggest parentheses around '&&' within '||'

Line 156:
|| ((c[4] == 'F') && (c[5] == 'E') && (c[6] == 'D'))

Any idea how to clear this up and why it only is triggered for that
specific line and not similar ones?

Zach
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      11-19-2010
Zach wrote:
) ll15.c: In function 'process_tokens':
) ll15.c:156: warning: suggest parentheses around '&&' within '||'
)
) Line 156:
) || ((c[4] == 'F') && (c[5] == 'E') && (c[6] == 'D'))
)
) Any idea how to clear this up and why it only is triggered for that
) specific line and not similar ones?

Have you considered the possibility that the compiler is a bit imprecise
in locating the exact line on which the error is ? Especially with
multi-line expression like this it's very difficult for a compiler to
pinpoint exactly where the error lies, especially if it's an error
like this one.

I'd guess it's a few lines up.



SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regexp question - look for parentheses then remove them Max Williams Ruby 8 10-09-2007 02:29 PM
"Can't use parentheses when calling a Sub" help...... zheetee@gmail.com ASP .Net 7 05-17-2006 04:44 PM
'AddressOf' operand must be the name of a method; no parentheses a =?Utf-8?B?UGF0cmljay5PLklnZQ==?= ASP .Net 15 10-18-2004 11:49 PM
Parentheses in the control expression Memana C++ 1 07-05-2004 02:37 PM
Empty parentheses Christopher Benson-Manica C Programming 10 10-17-2003 09:19 PM



Advertisments