CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2007
    Posts
    26

    Evaluation order of expression with single AND operator?

    I have an expression in C: if (aaa = bbb && (ccc = getc(stdin)) != ddd), and I need the first expression to be evaluated first. Does C, and for that matter other programming languages, always evaluate from left to right with a single boolean like this, and do the parentheses like I have on the right side mean that this side is evaluated first? Or does it depend on the compiler?

    I could just separate the expressions of course, but it is more compact this way.

  2. #2
    Join Date
    Jul 2009
    Location
    Gothenburg
    Posts
    12

    Re: Evaluation order of expression with single AND operator?

    See this page

    Programming languages mostly evaluate in left to right order. Parentheses group statements, causing the result contents of the parentheses to be evaluated as a single statement, but has no effect on the precedence order.

    In your case this means that the expression is evaluated as
    1. aaa = bbb
    2. If the result of 1. is true:
    3. ccc = getc(stdin)
    4. result of 3. is compared to ddd
    5. result of 1. is compared result of 2.
    Last edited by spacewarp; September 12th, 2010 at 06:39 AM.

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: Evaluation order of expression with single AND operator?

    Quote Originally Posted by codingnewbie View Post
    I could just separate the expressions of course, but it is more compact this way.
    Your code is not compact, it's crammed and that's something completely different.

    Don't cram code. There's no advantage. It's not going to be any faster, just harder to understand. And if you don't easily understand your own code, chances are others won't either. At least do the assignments separately.

    A good rule of thumb to avoid cramming is:

    One line - one thought.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Evaluation order of expression with single AND operator?

    Actually, that is equivalent to:
    Code:
    if (aaa = (bbb && ((ccc = getc(stdin)) != ddd)))
    Assuming that the built-in operator&& is involved here, then && introduces a sequence point. It is guaranteed that the subexpression bbb will be evaluated before ((ccc = getc(stdin)) != ddd), and that if bbb evaluates to false, ((ccc = getc(stdin)) != ddd) will not be evaluated. However, whether aaa is evaluated before bbb is implementation defined, since evaluation order is generally unspecified.

    EDIT:
    Quote Originally Posted by nuzzle
    It's not going to be any faster, just harder to understand.
    Agreed. spacewarp's mistake is an example of how these complex expressions can become harder to understand, even when one is fully attempting to understand it.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Evaluation order of expression with single AND operator?

    Quote Originally Posted by laserlight View Post
    Assuming that the built-in operator&& is involved here
    It has to be, you can not overload operators in C.

    Code:
    if (aaa = bbb && (ccc = getc(stdin)) != ddd)
    OP: That is incredibly complex, is there any way to expand that? Don't let the fact that C code doesn't get optimized fool you into thinking that expanding that a little will slow it down We have a word for code like that: obfuscated C
    Last edited by ninja9578; September 12th, 2010 at 03:27 PM.

  6. #6
    Join Date
    May 2007
    Posts
    26

    Re: Evaluation order of expression with single AND operator?

    Thanks for all the replies. I do realize it wont make it any faster, but I actually find it easier to read this way due to it being only one line rather than two, adding another level of indentation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured