Have a Prob in finding the output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AnagJohari
    New Member
    • May 2010
    • 96

    Have a Prob in finding the output

    I write the code....
    Code:
    void main()
    {
    int i=-3,j=2,k=0,m;
    m=++j&&++i||++k;
    printf("%d%d%d%d",i,j,k,m);
    getch();
    }
    THE out put of this program is -2301
    plz explain why -2301 comes.....
    thank you
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    First let me ask you: What do you think are the values of i, j, k, and m immediately before the printf statement?

    You will learn this better if you struggle through on your own a little.

    Comment

    • AnagJohari
      New Member
      • May 2010
      • 96

      #3
      Originally posted by donbock
      First let me ask you: What do you think are the values of i, j, k, and m immediately before the printf statement?

      You will learn this better if you struggle through on your own a little.
      according to me the value of i =-3 there is in increment in i in the expression so the value of i is incremented that means i=-2 there is an increment in j also so that the value of j is incremented to 3 but the value of k remains as it is it should be incremented because in the expression its incremented...
      & the value of m is 1 by using the truth table relation....... .
      that is my thinking about taking this program...
      but the thing is we use increment operator in the expression, is it actually change the value of i at their address when i print the valu i for j & k onwards.?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        When you have an OR, the compiler can evaluate the expression until it can determine whethere it is true or false. Then it can stop evaluation. In your case, the expression is true always because the values in i and j are always true. The makes AND of i an j always true and this makes the OR always true.

        Therefore, there is no need to evaluate k.

        Worse, the order of evaluation depends on the compiler. You may find this works as you expect on another compiler if it evaluates all lf the variables first and then applies the logical operators. However, the compiler is free to evaluate the variables in an expression in any order.

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          However, the compiler is free to evaluate the variables in an expression in any order.
          Afaik in the case of logical || and && the compiler has no choice - it evaluates the left expression first, and if it is true (false), doesn't evaluate the right one.
          E.g.
          dosomething() || exit();

          Comment

          • AnagJohari
            New Member
            • May 2010
            • 96

            #6
            Originally posted by newb16
            Afaik in the case of logical || and && the compiler has no choice - it evaluates the left expression first, and if it is true (false), doesn't evaluate the right one.
            E.g.
            dosomething() || exit();
            tnx i got the point & now i understand how this ans comes

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              You should read up on short-circuit evaluation.

              Comment

              Working...