Output of program

This is a discussion on Output of program within the C Programming forums, part of the General Programming Boards category; Hello, When I executed this program I got output -2 3 0 1. Can anyone explain what how this calculation ...

  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
    22,170
    You should make an attempt at explaining it yourself. Think of what you know about the operators involved.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,170
    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 04:01 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 06:24 AM
  2. calling an external program + capture output?
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 03-21-2008, 01: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, 09:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21