Thread: How i got this output?

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    INDIA
    Posts
    3

    Unhappy How i got this output?

    Hi al,

    I am a newbie to this forum...i had a problem in understanding the output which i got it when i compiled in Turbo C..
    Code:
    main()
    {
      int i=-3,j=2,k=0,m;
      m= ++i&&++j||++k;
      printf("\n i=%d j=%d k=%d m=%d",i,j,k,m); 
    }
    i got the output as i=-2 j=3 k=0 m=1;

    i thought the value of k would be 1..but it is printing as 0..when i changed the OR operator to some other operator i got the k value to be 1...why i am getting 0 in this case..Pls clarify my doubt

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Short circuit/lazy evaluation: when the left operand of || evaluates to true (i.e., non-zero), the right operand is not evaluated since it can be determined that the result of the expression is true. Likewise, when the left operand of && evaluates to false (i.e., zero), the right operand is not evaluated.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2009
    Location
    INDIA
    Posts
    3
    thanks for your reply laserlight...
    I understood. But according to the operator precedence table "++" (prefix)increment has top priority than Logical AND (&&) and Logical OR(||)..
    So in that case increment part should evaluate 1st then only logical operations should be evaluated.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The || operator (and && operator) introduces a sequence point, hence the right operand cannot begin to be evaluated until the left operand has been completely evaluated. That said, note that precedence and order of evaluation are not the same thing, though precedence might dictate the order of evaluation in some cases.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2009
    Location
    INDIA
    Posts
    3
    i am confused with precedence and order of evaluation...can anyone explain me with an example...

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Please read on wiki about operator precedence in C/ C++

    i can give you an example that.

    Do u know BODMAS ???

    / is having greater precedence than *,
    * is having greater precedence than +,
    + is having greater precedence than -,


    above are some example. Further u can study on net.

    Lets say in a statement u wrote

    a = b * c + d;

    than first * will happen, than addition. Did u got ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM