some1 tell me wats goin on....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • confusedbigtime
    New Member
    • Feb 2007
    • 3

    some1 tell me wats goin on....

    Hi there...im new to C programmin and ive gotten a little stuck with precedence of C operators...it wud be great if some1 cud tell me how the compiler goes about solving these kinds of expressions....

    i=0;
    7||0&&++i;
    why doesnt the value of i change???
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by confusedbigtime
    Hi there...im new to C programmin and ive gotten a little stuck with precedence of C operators...it wud be great if some1 cud tell me how the compiler goes about solving these kinds of expressions....

    i=0;
    7||0&&++i;
    why doesnt the value of i change???
    && (AND) has a higher precedence than || (OR) so the expression
    Code:
    7||0&&++i;
    is evaluated
    Code:
    7||(0&&++i);
    remember unlike arithmetic operators C guarantees that the operands of binary logical operators are evaluated from left to right (after any precedence) so in 0&&++i the 0 (false) will be evaluated first. In addition, if the final result of the expression can be determined from the value of first operand (in this case false) the second operand (int this case ++i) is not evaluated. Thus the value of i is not changed because the ++i is never evaluated because 0 the first operand of && is false

    Comment

    • confusedbigtime
      New Member
      • Feb 2007
      • 3

      #3
      Originally posted by horace1
      && (AND) has a higher precedence than || (OR) so the expression
      Code:
      7||0&&++i;
      is evaluated
      Code:
      7||(0&&++i);
      remember unlike arithmetic operators C guarantees that the operands of binary logical operators are evaluated from left to right (after any precedence) so in 0&&++i the 0 (false) will be evaluated first. In addition, if the final result of the expression can be determined from the value of first operand (in this case false) the second operand (int this case ++i) is not evaluated. Thus the value of i is not changed because the ++i is never evaluated because 0 the first operand of && is false
      Hi horace!!
      I get what you said and in fact i came up with this explanation myself initially....wh at got me really confused is this theory doesnt seem to hold for the following code; its pretty much the same code, with the zero changed to a 1:
      int i=0;
      7||1&&++i;
      of what i gathered from your post, the compiler goes about this expression in the following way :
      && has higher precedence compared to ||, so we have 7||(1&&++i). while tryin to evaluate 1&&++i (i reckon theres is no short circuit evaluation that comes into the picture here) the value of i should have changed. However its value still remains 0!!This is precisely what is confusing me.....

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by confusedbigtime
        Hi horace!!
        I get what you said and in fact i came up with this explanation myself initially....wh at got me really confused is this theory doesnt seem to hold for the following code; its pretty much the same code, with the zero changed to a 1:
        int i=0;
        7||1&&++i;
        of what i gathered from your post, the compiler goes about this expression in the following way :
        && has higher precedence compared to ||, so we have 7||(1&&++i). while tryin to evaluate 1&&++i (i reckon theres is no short circuit evaluation that comes into the picture here) the value of i should have changed. However its value still remains 0!!This is precisely what is confusing me.....
        thinking about it again we rememebr C guarantees that the operands of binary logical operators are evaluated from left to right. so
        Code:
        7||(0&&++i);
        7||(1&&++i).
        in both cases the || is evaluated and it true so (1&&++i) or (0&&++i) is never eveluated, so i = 0
        now consider
        Code:
        0||1&&++i;
        0 is false so 1&&++i is evaluated
        1 is true so ++i is evaluated (i is incremented to 1)
        and the overall expression is true
        now
        Code:
        0||0&&++i;
        0 is false so 0&&++i is evaluated
        0 is false so ++i is not evaluate
        the overall expression is false

        do you rekon we are there now?

        Comment

        • confusedbigtime
          New Member
          • Feb 2007
          • 3

          #5
          Originally posted by horace1
          thinking about it again we rememebr C guarantees that the operands of binary logical operators are evaluated from left to right. so
          Code:
          7||(0&&++i);
          7||(1&&++i).
          in both cases the || is evaluated and it true so (1&&++i) or (0&&++i) is never eveluated, so i = 0
          now consider
          Code:
          0||1&&++i;
          0 is false so 1&&++i is evaluated
          1 is true so ++i is evaluated (i is incremented to 1)
          and the overall expression is true
          now
          Code:
          0||0&&++i;
          0 is false so 0&&++i is evaluated
          0 is false so ++i is not evaluate
          the overall expression is false

          do you rekon we are there now?
          oh right.......got it now....thanks a lot.....

          Comment

          Working...