Thread: what will be the output of this code snippet

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    21

    what will be the output of this code snippet

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

    BUT pls explain HOW ?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Quote Originally Posted by devilofwar View Post
    Code:
    int main(){
    int i = -3,j=2,k=0,m;
    m=++i||++j&&++k;
    printf("%d%d%d%d",i,j,k,m);
    return 0;
    }
    the ans is -2 2 0 1

    BUT pls explain HOW ?
    I think you get -2 because I is I++ which adds 1. J is declared at top, same with J. M is orignally declared as 0. but its got a ++ increment so 1 is added to it.

    Could someone confirm if im right ? if so it proves my revision for my programming test on wednesday is going well lol.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    21
    no ur wrong cause the ans will depend on the evaluation order of this stmt which is unclear to me

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by devilofwar View Post
    Code:
    int main(){
    int i = -3,j=2,k=0,m;
    m=++i||++j&&++k;
    printf("%d%d%d%d",i,j,k,m);
    return 0;
    }
    the ans is -2 2 0 1

    BUT pls explain HOW ?
    First the || has the left-to-right precedence. so you increase i first to get i =-2. since it's an 'or' statement, and you're not comparing ++i with anything, so ++i is true, it won't look at the second expression on the right which icnreatement j and k; thus j and k retains their original values. Then, since the logical test is true, the test return "1", which is then assigned to m, thus m gets the value of 1.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    21
    even i thought of this explanation But have u considered the fact that && gets evaluated first and then || gets evaluated
    Precedence table frm anywhere says this

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    21
    Quote Originally Posted by nimitzhunter View Post
    First the || has the left-to-right precedence. so you increase i first to get i =-2. since it's an 'or' statement, and you're not comparing ++i with anything, so ++i is true, it won't look at the second expression on the right which icnreatement j and k; thus j and k retains their original values. Then, since the logical test is true, the test return "1", which is then assigned to m, thus m gets the value of 1.
    even i thought of this explanation But have u considered the fact that && gets evaluated first and then || gets evaluated
    Precedence table frm anywhere says this

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    int i = -3,j=2,k=0,m;
    m=++i || ++j && ++k;
    
    printf("%d%d%d%d",i,j,k,m);
    the ans is -2 2 0 1

    Ok... this is not bitwise math... so...

    The first thing on the right of the equals sign is ++i ... i now equals -2
    The second step is the logical OR... which is completed ...
    ++i succeeded and evaluates to TRUE thus m = 1
    Because the first OR is true, the remainder of the statement is never executed so the values of j and k are unchanged.

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    21
    but here why is && not evaluated before the || operator ? should the && be evaluated and then ||

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by devilofwar View Post
    but here why is && not evaluated before the || operator ? should the && be evaluated and then ||
    It can't be... or the values of j and k would be changed.

    Evaluation ends when the OR clause succeeds.

  10. #10
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    http://cboard.cprogramming.com/c-pro...ce-%7C%7C.html
    Check out that link. They discuss the same problem.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > But have u considered the fact that && gets evaluated first and then || gets evaluated
    But it doesn't.

    Since you mentioned precedence, let's put some extra ( ) in for clarity.
    > m=++i || ++j && ++k;
    becomes
    > m=++i || (++j && ++k);

    Now regardless of anything else, the boolean operators ALWAYS evaluate the left term first, so ++i is going to happen first. Since that evaluates to true (non-zero), that's the end of the show. The LHS is true, therefore the whole thing MUST be true and the whole RHS of (++j && ++k) is skipped.

    Now, if you had this instead, something different would happen.
    > m= (++i || ++j ) && ++k;
    Can you work this one out?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Dec 2010
    Posts
    21
    Quote Originally Posted by Salem View Post
    > But have u considered the fact that && gets evaluated first and then || gets evaluated
    But it doesn't.

    Since you mentioned precedence, let's put some extra ( ) in for clarity.
    > m=++i || ++j && ++k;
    becomes
    > m=++i || (++j && ++k);

    Now regardless of anything else, the boolean operators ALWAYS evaluate the left term first, so ++i is going to happen first. Since that evaluates to true (non-zero), that's the end of the show. The LHS is true, therefore the whole thing MUST be true and the whole RHS of (++j && ++k) is skipped.

    Now, if you had this instead, something different would happen.
    > m= (++i || ++j ) && ++k;
    Can you work this one out?

    in this case i =-2 j= 2 and k= 1

    and yes the link provided above was helpful !!

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Just a note: real programmers don't write lousy code like that. They are explicit in what they intend and use parentheses (and variable names that make sense) to ensure the behavior is as expected.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Figure out the output of code ...
    By Newworld in forum C Programming
    Replies: 5
    Last Post: 09-19-2004, 10:32 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 01:27 PM
  5. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM