Like Tree3Likes

what is the value of this expression?

This is a discussion on what is the value of this expression? within the C Programming forums, part of the General Programming Boards category; what is the output of the following c code: int i=-3,j=2,k=0,m; m= ++i||++j&&++k; printf("%d%d%d%d",i,j,k,m); please also tell the order of ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    91

    what is the value of this expression?

    what is the output of the following c code:

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

    please also tell the order of evaluation

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    Write a program to find out. With the output, figure it out, then come back here for a confirmation.
    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
    Aug 2011
    Posts
    91
    Quote Originally Posted by laserlight View Post
    Write a program to find out. With the output, figure it out, then come back here for a confirmation.
    mam,i know the output. its -2,2,0,1

    but i dont know how its coming.

    according to me:

    since && has higher precedence than ||,that part of the expression will be evaluated first so,
    j becomes 3 and k becomes 1
    (3&&1)=1(since both are positive).

    now i is evaluated so it becomes -2

    -2||1 = 1
    so m =1

    so the output should be -2 3 1 1. but its wrong can you tell me why?

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by theju112 View Post
    i know the output. its -2,2,0,1

    but i dont know how its coming.

    according to me:

    since && has higher precedence than ||,that part of the expression will be evaluated first so,
    j becomes 3 and k becomes 1
    (3&&1)=1(since both are positive).

    now i is evaluated so it becomes -2

    -2||1 = 1
    so m =1

    so the output should be -2 3 1 1. but its wrong can you tell me why?
    No, they get evaluated in order. (I was wrong in saying that they had the same precedence)
    So, a short circuit happens here and the RHS never gets evaluated (seeing that the LHS is already true).
    Last edited by manasij7479; 08-12-2011 at 10:17 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    Quote Originally Posted by theju112
    since && has higher precedence than ||,that part of the expression will be evaluated first
    No, precedence determines grouping, not order of evaluation. We could make the grouping more explicit by introducing parentheses:
    Code:
    m = ++i || (++j && ++k);
    Nonetheless, it is the ++i that is evaluated first because of the sequence point introduced by operator||

    EDIT:
    Quote Originally Posted by manasij7479
    No the precedence is same
    No, theju112 is correct to say that && has a higher precedence than ||.
    Last edited by laserlight; 08-12-2011 at 10:13 AM.
    theju112 likes this.
    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
    Aug 2011
    Posts
    91
    cool!! that makes perfect sense...
    but just hav a look at this wiki page:
    Operators in C and C++ - Wikipedia, the free encyclopedia
    here its given that
    && has precedence 13 and || has 14 that's how i started evaluating the exp.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    oh boy its a lot complicated than i imagined.

    c++ witch,so ur saying that all side effects of an expression should be executed before a sequence point right?
    so i++ is evaluated giving a negative value of -2.since its -ve,the remainder of the expression has to be evaluated .now the part:
    ++j&&++k are evaluated
    so j would be 3 and k becomes 1.
    even that is not the correct output.
    now what??

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Google "short circuit evaluation in C". This will explain what is going on.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    hello??????anybody there ??

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Did you use google? Bad form to bump threads with "Pay attention to me posts". Short circuit evaluation my friend means that the sequence point introduced by || causes the program to figure out the answer without doing the all the math, essentially. Thus on the example since the left hand side is true, it doesn't bother doing anything with the right hand side.
    laserlight and theju112 like this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    sorry about that last post guys...

    andrewhunter thanks for that perfect answer.
    what i thought was that a -ve value is also a false value just like a zero is false in c.

    now one last doubt:

    a sequence point causes all side effects to be executed without consideration of precedence just as it happened in this example.
    am i right?

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    The misunderstanding you are having is what 'precedence' refers to when it comes to these boolean operators. Take a look at Laser's post #5. That post clearly demonstrates that precedence here refers to grouping not order of evaluation.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    ok..that explains it..so precedence is used for grouping of terms in an expression and not for deciding the order in which they are
    evaluated.once the terms are grouped the evaluation is done.now am i right?

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Basically........

    As has been discussed the precedence determines grouping, which basically means it decides where the sequence point is. So in the above example we have x || y && z. Based on precendence the sequence points would be generated as:

    x <sequence point> operand2

    then you would have

    y <sequence point> z.

    Now for the evaluation of the sequence points, we have this:
    Quote Originally Posted by C99 6.5.13/14
    Logical AND:
    Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation;
    there is a sequence point after the evaluation of the first operand. If the first operand
    compares equal to 0, the second operand is not evaluated.

    Logical OR:
    Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; there is
    a sequence point after the evaluation of the first operand. If the first operand compares
    unequal to 0, the second operand is not evaluated.
    And as we can see, short circuiting is guaranteed by the standard, and thus in this example anything after the first sequence point is not evaluated.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    i guess this is not beginner stuff as i expected.

    Quote Originally Posted by AndrewHunter View Post
    Basically........

    As has been discussed the precedence determines grouping, which basically means it decides where the sequence point is. So in the above example we have x || y && z. (Based on precendence the
    sequence points would be generated as):


    x <sequence point> operand2

    then you would have

    y <sequence point> z.
    here i go again: now im having doubts with that sentence i have bracketed in your re above.

    as far as i know a sequence point is generated at && and || operators. so are we really considering precedence here for anything?

    now i just want to know this : (x||y)&&z is wrong grouping and the right grouping is : x||(y&&z).is this correct?

    after this grouping is done,x is evaluated and it is non-zero.so evaluation stops there and thats it.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initializer expression list treated as compound expression
    By karthikeyanvisu in forum C Programming
    Replies: 7
    Last Post: 02-26-2011, 04:19 PM
  2. Replies: 2
    Last Post: 11-25-2009, 06:38 AM
  3. regular expression
    By jackel7777 in forum C Programming
    Replies: 4
    Last Post: 08-16-2007, 03:02 AM
  4. expression question
    By matthughes in forum C Programming
    Replies: 4
    Last Post: 05-16-2007, 11:08 PM
  5. S-Expression
    By stimpyzu in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2004, 07:15 PM

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