parentheses question

Discussion in 'C Programming' started by Zach, Nov 19, 2010.

  1. Zach

    Zach Guest

    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
     
    Zach, Nov 19, 2010
    #1
    1. Advertising

  2. Zach

    Zach Guest

    On Nov 19, 7:08 am, pete <> 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
     
    Zach, Nov 19, 2010
    #2
    1. Advertising

  3. Zach

    Zach Guest

    On Nov 19, 7:22 am, pete <> wrote:
    > Zach wrote:
    >
    > > On Nov 19, 7:08 am, pete <> 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
     
    Zach, Nov 19, 2010
    #3
  4. Zach

    Willem Guest

    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
     
    Willem, Nov 19, 2010
    #4
  5. Zach

    Zach Guest

    On Nov 19, 8:20 am, Willem <> 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
     
    Zach, Nov 19, 2010
    #5
  6. Zach

    Zach Guest

    On Nov 19, 8:20 am, Willem <> 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
     
    Zach, Nov 19, 2010
    #6
  7. In article <>,
    Zach <> wrote:
    >On Nov 19, 8:20 am, Willem <> 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 -
     
    Kenny McCormack, Nov 19, 2010
    #7
  8. Zach

    BartC Guest

    "Zach" <> wrote in message
    news:...
    > On Nov 19, 8:20 am, Willem <> 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
     
    BartC, Nov 19, 2010
    #8
  9. Zach

    Zach Guest

    On Nov 19, 8:37 am, (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
     
    Zach, Nov 19, 2010
    #9
  10. Zach

    Willem Guest

    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
     
    Willem, Nov 19, 2010
    #10
  11. Zach

    Zach Guest

    On Nov 19, 9:14 am, Willem <> wrote:
    >
    > 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.


    Thanks for the suggestion.

    Zach
     
    Zach, Nov 19, 2010
    #11
  12. 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.


    Because you've got here (when indentated correctly)

    ((c[4] == 'F' || 'R' || 'O' || 'K')
    && (isdigit(c[5]) || islower(c[5]))
    || ((c[4] == 'A') && (c[5] == 'L') && (c[6] == 'L'))
    || ...

    So it's

    A && B || C || ...

    just as the compiler tells you.

    Others have already pointed out that

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

    doesn't make sense since it's always true. I guess you meant
    to ask if c[4] is either F, R, O or K, didn't you?

    And why make it rather difficult to understand if there are
    function that can do the job for parts of what you want? If
    I'm not mistaken

    if ( strchr( "FROK", c[ 0 ] )
    && ( isdigit( c[ 1 ] ) || islower( c[ 1 ] ) )
    && ! strncmp( c + 2, "->", 2 )
    && ( ( strchr( "FROK", c[ 4 ] )
    && ( isdigit( c[ 5 ] ) || islower( c[ 5 ] ) ) )
    || ! strncpm( c + 4, "ALL", 3 )
    || ! strncmp( c + 4, "FED", 3 )
    || ! strncmp( c + 4, "ROM", 3 )
    || ! strncmp( c + 4, "ORI", 3 )
    || ! strncmp( c + 4, "KLI", 3 ) ) )

    would do the same (if my assumption about what you really in-
    tended in that broken line(s) is correct) and I would consider
    it to be a bit easier to grasp. I might even go a step further
    and define a few helper functions like

    bool
    is_FROK_token( const char *s )
    {
    return strchr( "FROK", s[ 0 ] )
    && ( isdigit( s[ 1 ] ) || islower( s[ 1 ] ) );
    }

    bool
    is_pointing( const char * s )
    {
    return ! strncmp( s, "->", 2 );
    }

    bool
    is_word( const char * s )
    {
    return ! strncpm( s, "ALL", 3 )
    || ! strncmp( s, "FED", 3 )
    || ! strncmp( s, "ROM", 3 )
    || ! strncmp( s, "ORI", 3 )
    || ! strncmp( s, "KLI", 3 );
    }

    Then you could write

    if ( is_FROK_TOKEN( c )
    && is_pointing( c + 2 )
    && ( is_FROK_token( c + 4 )
    || is_word( c + 4 ) ) )

    to make your intensions even clearer.

    BTW, instead of writing a parser by hand you might find having
    a look at tools like flex and bison beneficial - they take a bit
    of time getting used to but then can make analysing/parsing struc-
    tured data a lot easier and more fun.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Nov 19, 2010
    #12
  13. Zach

    Zach Guest

    On Nov 19, 9:48 am, (Jens Thoms Toerring) wrote:
    >
    > BTW, instead of writing a parser by hand you might find having
    > a look at tools like flex and bison beneficial - they take a bit
    > of time getting used to but then can make analysing/parsing struc-
    > tured data a lot easier and more fun.


    Hallo Jens,

    Interesting. Thanks for the ideas. I will look into it.

    Zach
     
    Zach, Nov 19, 2010
    #13
  14. Zach <> writes:
    > 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.


    Here's a code fragment that produces the same warning:

    int a = 1, b = 1, c = 1;
    int d = a || b && c;

    The expression
    a || b && c
    is equivalent to
    a || (b && c)
    because "&&" has higher precedence than "||". The compiler is
    suggestinging that depending on this rule tends to make code unclear.
    Reading the expression, it's hard to tell whether the author really
    meant that, or
    (a || b) && c
    Adding parentheses around "b && c), while not changing the meaning
    of the expression, would make it clearer.

    Yes, the original code is valid; that's why you got a warning, not an
    error message.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 19, 2010
    #14
  15. Zach

    Zach Guest

    On Nov 19, 11:42 am, Keith Thompson <> wrote:
    >
    > The expression
    >     a || b && c
    > is equivalent to
    >     a || (b && c)
    > because "&&" has higher precedence than "||".  The compiler is
    > suggestinging that depending on this rule tends to make code unclear.
    > Reading the expression, it's hard to tell whether the author really
    > meant that, or
    >     (a || b) && c
    > Adding parentheses around "b && c), while not changing the meaning
    > of the expression, would make it clearer.
    >
    > Yes, the original code is valid; that's why you got a warning, not an
    > error message.


    I see. Thanks for explaining Keith.

    Zach
     
    Zach, Nov 19, 2010
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    'AddressOf' operand must be the name of a method; no parentheses a

    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Oct 18, 2004, in forum: ASP .Net
    Replies:
    15
    Views:
    7,656
    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=
    Oct 19, 2004
  2. Replies:
    7
    Views:
    9,293
    Jeff Dillon
    May 17, 2006
  3. Memana
    Replies:
    1
    Views:
    325
    Alf P. Steinbach
    Jul 5, 2004
  4. Christopher Benson-Manica

    Empty parentheses

    Christopher Benson-Manica, Oct 17, 2003, in forum: C Programming
    Replies:
    10
    Views:
    618
    Jeremy Yallop
    Oct 17, 2003
  5. Max Williams
    Replies:
    8
    Views:
    114
    Max Williams
    Oct 9, 2007
Loading...

Share This Page