Parentheses Ineffectual

Discussion in 'C Programming' started by Frederick Gotham, Jul 9, 2006.

  1. If you wish to override the natural operator precedence and associativity
    in C, you can use parentheses:

    5 * (4 - (2 + 7))

    However, I was surprised to discover (by testing) yesterday that some
    operators are unaffected by parentheses. I expected the following program
    to print:

    Func2() called, returns 0.
    Func1() called, returns 0.


    However, with my own compiler, it prints:

    Func1() called, returns 0.


    The parentheses used in "main" have no effect on the && operator.


    #include <stdio.h>

    int Func1(void)
    {
    puts("Func1 called, returns 0.");

    return 0;
    }

    int Func2(void)
    {
    puts("Func2 called, returns 0.");

    return 0;
    }

    int Func3(void)
    {
    puts("Func3 called, returns 0.");

    return 0;
    }

    int main(void)
    {
    Func1() && ( Func2() && Func3() );
    }


    Any thoughts on this?

    --

    Frederick Gotham
    Frederick Gotham, Jul 9, 2006
    #1
    1. Advertising

  2. In article <159sg.11279$>,
    Frederick Gotham <> wrote:

    >However, I was surprised to discover (by testing) yesterday that some
    >operators are unaffected by parentheses. I expected the following program
    >to print:



    >int main(void)
    >{
    > Func1() && ( Func2() && Func3() );
    >}


    >Any thoughts on this?


    The && operator is defined such that the left operand is executed
    and its value tested, and the right operand is ignored if the
    left operand it false. This allows constructs such as
    x != 5 && (1/(x-5) < y)
    to be well defined.


    Always remember that parentheses do not mean that the indicated
    section must be executed first out of all of the terms in the
    expression: the parenthesis indicate groupings and need not be
    paid attention to until the value of the grouping is required for
    the connecting operator.

    For instance, in your example, 5 * (4 - (2 + 7))
    this does not mean that (4 - (2 + 7)) must be evaluated first:
    the two operands of * are at equal priority and the compiler may
    evaluate either one of them first. It is free to evaluate the 5 first
    and only then proceed to (4 - (2 + 7)). Inside the outer (),
    the compiler can execute the operands of the - in either order,
    possibly executing the 4 first before proceeding on to the (2+7).

    You can experiment with this by using, for example,

    int p(int x) { printf("p argument was %d\n", x); x }

    p(5) * (p(4) - (p(2) + p(7)))
    --
    There are some ideas so wrong that only a very intelligent person
    could believe in them. -- George Orwell
    Walter Roberson, Jul 9, 2006
    #2
    1. Advertising

  3. Frederick Gotham

    Malcolm Guest

    "Frederick Gotham" <> wrote in message
    news:159sg.11279$...
    >
    > If you wish to override the natural operator precedence and associativity
    > in C, you can use parentheses:
    >
    > 5 * (4 - (2 + 7))
    >
    > However, I was surprised to discover (by testing) yesterday that some
    > operators are unaffected by parentheses. I expected the following program
    > to print:
    >
    > Func2() called, returns 0.
    > Func1() called, returns 0.
    >
    >
    > However, with my own compiler, it prints:
    >
    > Func1() called, returns 0.
    >
    >
    > The parentheses used in "main" have no effect on the && operator.
    >
    >
    > #include <stdio.h>
    >
    > int Func1(void)
    > {
    > puts("Func1 called, returns 0.");
    >
    > return 0;
    > }
    >
    > int Func2(void)
    > {
    > puts("Func2 called, returns 0.");
    >
    > return 0;
    > }
    >
    > int Func3(void)
    > {
    > puts("Func3 called, returns 0.");
    >
    > return 0;
    > }
    >
    > int main(void)
    > {
    > Func1() && ( Func2() && Func3() );
    > }
    >
    >
    > Any thoughts on this?
    >

    The compiler must do this. The left most expression of an AND or OR is
    executed first. If the result of the expression can be determined, the
    second half may not be executed.
    Thus you can say

    if( x > 0 && y/x > 100)

    in the knowledge that the expression y/x will never cause a divide by zero.

    --
    Buy my book 12 Common Atheist Arguments (refuted)
    $1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
    Malcolm, Jul 9, 2006
    #3
  4. Frederick Gotham <> wrote:
    >However, I was surprised to discover (by testing) yesterday that some
    >operators are unaffected by parentheses. I expected the following program
    >to print:
    >
    > Func2() called, returns 0.
    > Func1() called, returns 0.
    >
    >
    >However, with my own compiler, it prints:
    >
    > Func1() called, returns 0.
    >
    >The parentheses used in "main" have no effect on the && operator.
    >
    > ...( Func1,2 &3 deleted)
    >
    >int main(void)
    >{
    > Func1() && ( Func2() && Func3() );
    >}
    >
    >Any thoughts on this?


    Your expectations are wrong:

    -------------------------------------
    ISO/IEC 9899:1999(E)

    6.5.13 Logical AND operator
    ....
    Semantics
    ....
    4 Unlike the bitwise binary & operator, the && operator guarantees
    left-to-right evaluation; there is a sequence point after the
    evaluation of the first operand. If the first operand compares equal
    to 0, the second operand is not evaluated.
    ---------------------------------------

    Had you used the '&' operator instead, you would see the output of all
    three functions in *any* order. All three operands would have been
    evaluated. The order of evaluation of the operands is not the order of
    evaluation of the operators. It is only the later the one that can be
    influenced by the use of parenthesis.
    Roberto Waltman, Jul 9, 2006
    #4
    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,549
    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=
    Oct 19, 2004
  2. Replies:
    7
    Views:
    9,212
    Jeff Dillon
    May 17, 2006
  3. Memana
    Replies:
    1
    Views:
    319
    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:
    600
    Jeremy Yallop
    Oct 17, 2003
  5. Replies:
    44
    Views:
    937
    Timo Virkkala
    Jan 6, 2005
Loading...

Share This Page