Thread: Output of program

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    16

    Output of program

    Hello,

    When I executed this program I got output -2 3 0 1. Can anyone explain what how this calculation is reached in program below. Thanks

    Code:
    int main(){
    int i = -3, j = 2, k = 0, m;
    m = ++i && ++j || ++k;
    printf("%d%d%d%d", i,j,k,m);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should make an attempt at explaining it yourself. Think of what you know about the operators involved.
    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
    Apr 2010
    Posts
    16
    I have put this question for help. If you know answer tell it to me.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    23
    Code:
    m = ++i && ++j || ++k;
    the logical operators && and || are short-circuit operators with left to right associativity. so in above expression, evaluation starts from the left most variable, i.e. i, and goes left till the truth value of the expr is not determined. as soon as the truth value is found, the furthur evaluation of expression stops.

    so in above expression, we have

    m = (++i) && (++j) || (++k)

    starting from left we first evaluate the && along with it's operands. in C, any non-zero value is logically true. therefore (++i) && (++j) evaluates true, as their values during comparison can never be zero due to their initial values, and the truth value is also determined for the whole expression ( true || 0 == true). so no further evaluation takes place and the statement ++k is not executed as control never reaches that statement.

    note that during the comparison the exact values of i and j cannot be determined as in an expression, only precedence and associativity rules are guaranteed, not the order of evaluation of sub-expressions like ++i. however, after the above expr, the values of i and j have been incremented as they were evaluated, even if their order was not known.

    finally, m is set to the logical result of the expression, which is equal to 1.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Stuart Dickson
    I have put this question for help. If you know answer tell it to me.
    I can see that, and I know the answer. But you should show us that you have attempted to answer the question yourself.

    In particular, read the homework guidelines. If you keep getting spoonfed answers like what xabhi did for you, then you are just wasting your time (and ours). You should be learning how these things work from books and tutorials, and then making use of these examples to see if you can apply your knowledge to deepen your understanding. Where we can best help you is where you have some gap or misunderstanding that can be corrected.

    EDIT:
    Quote Originally Posted by xabhi
    starting from left we first evaluate the && along with it's operands.
    Actually, because && introduces a sequence point the left hand side, i.e., ++i, is evaluated first. Since the result is non-zero, the right hand side of the expression is evaluated.

    Quote Originally Posted by xabhi
    note that during the comparison the exact values of i and j cannot be determined as in an expression, only precedence and associativity rules are guaranteed, not the order of evaluation of sub-expressions like ++i. however, after the above expr, the values of i and j have been incremented as they were evaluated, even if their order was not known.
    It is generally true that the order of evaluation of subexpressions is unspecified, but in this case we can determine the order of evaluation by inspection because of the sequence points that are introduced.
    Last edited by laserlight; 09-04-2010 at 03:01 AM.
    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

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    Thanks a lot xabhi. Actually where I made mistake was that I was treating negative value as false.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    23
    It is generally true that the order of evaluation of subexpressions is unspecified, but in this case we can determine the order of evaluation by inspection because of the sequence points that are introduced.
    clearly missed these points... thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-29-2010, 05:24 AM
  2. calling an external program + capture output?
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 03-21-2008, 12:49 AM
  3. program looping with final output
    By hebali in forum C Programming
    Replies: 24
    Last Post: 02-28-2008, 10:58 AM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  5. Redirecting program output straight to an edit control
    By bennyandthejets in forum C++ Programming
    Replies: 5
    Last Post: 07-05-2004, 08:25 AM