A good question about precedence

This is a discussion on A good question about precedence within the C Programming forums, part of the General Programming Boards category; Hi guys here is my question: let a=b=c=10 and what is the value of a,b,c after --a||--b&&--c I think it ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    46

    A good question about precedence

    Hi guys here is my question:

    let a=b=c=10 and what is the value of a,b,c after --a||--b&&--c


    I think it should have been b=9 and c= 9.I think it should follow this pattern: ((--a)||((--b)&&(--c)))

    is there anyone who can help me ?
    Last edited by dayanike; 03-13-2012 at 06:35 AM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,172
    I rate this question as "average".

    You are missing a `-' in what I assume is supposed to be "--b".

    Soma

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    46
    Quote Originally Posted by phantomotap View Post
    I rate this question as "average".

    You are missing a `-' in what I assume is supposed to be "--b".

    Soma
    yes you are right it should be --b

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,172
    This question has nothing to do with precedence or associativity.

    Look around for articles on "short circuit operators" and find an answer.

    Soma

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    phantomotap is right, this question has nothing to do with operator precedence.
    And what exactly do you want help with? Does your answer not match the textbook?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    46
    Quote Originally Posted by QuantumPete View Post
    phantomotap is right, this question has nothing to do with operator precedence.
    And what exactly do you want help with? Does your answer not match the textbook?

    What i tried to say is because of the higher precedence of && operator,firstly (--b)&&(--c) should have been executed and then the result of (--b)&&(--c) match with --a.I mean after all operations b should be 9 c should be 9 but the textbook says the evaluation proceeds from left to right although && has higher precedence than || so according to textbook a=9, b=10 and c=10

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,172
    O_o

    Well, your book is either wrong or, much more likely, you misunderstood.

    Operators precedence and associativity are simply two different things.

    The binary boolean logic operators are left to right associative: (first_here && then_here).

    The binary boolean logic operator "AND" has a higher precedence than binary boolean logic operator "OR": (((stuff_here) && (stuff_there)) || (now_here)).

    These things are both important; they don't have anything to do with each other.

    You could read the above by duplicating semantics for a possibly better explanation of what you aren't getting.

    The binary boolean logic operator "AND" is left-to-right associative and has a higher precedence than binary boolean logic operator "OR" which also is left-to-right associative: (first_here || (then_here && and_here)).

    Soma
    Last edited by phantomotap; 03-13-2012 at 07:46 AM. Reason: context

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    46
    Quote Originally Posted by phantomotap View Post
    O_o

    Well, your book is either wrong or, much more likely, you misunderstood.

    Operators precedence and associativity are simply two different things.

    The binary boolean logic operators are left to right associative.

    The binary boolean logic operator "AND" has a higher precedence than binary boolean logic operator "OR".

    These things are both important; they don't have anything to do with each other.

    You could read the above by duplicating semantics for a possibly better explanation of what you aren't getting.

    The binary boolean logic operator "AND" is left-to-right associative and has a higher precedence than binary boolean logic operator "OR" which also is left-to-right associative.

    Soma
    thanks a lot for your explanations.According to you what is the final values of a,b and c ?

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,172
    I'm not going to answer that question.

    Answering an instance of a question will do you no good. I don't ........ing care to waste my time in that fashion.

    I'm only posting to help you understand why you don't know the answer to it.

    Now, what do you think they should be and why?

    Soma

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    Quote Originally Posted by dayanike
    According to you what is the final values of a,b and c ?
    Why not write a program and find out for yourself?
    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

  11. #11
    Registered User
    Join Date
    Feb 2012
    Posts
    46
    I've already did dont worry but I try to understand whather ı misunderstood or the textbook is wrong.

  12. #12
    Registered User
    Join Date
    Feb 2012
    Posts
    46
    I wrote what i think in my first post check it. It should be b=9 and c=9.

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,172
    O_o

    Read post 4!

    I can't make this any more clear: your interpretation of the condition is correct while your expectation of the values is wrong.

    Soma

  14. #14
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    The result is:
    a = 9, b = 10, c = 10 because the expression overall is "logical". It is evaluated only as far as required to determine if the result will be true or false. After the first --a, the intermediate result is 9 which is considered TRUE (non zero). Since the connecting operation is logical OR, the entire expression is already TRUE no matter what follows. Therefore there is no need to evaluate more terms... and therefore the 'b' and 'c' variables are untouched.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of Precedence question
    By Jony in forum C Programming
    Replies: 10
    Last Post: 08-22-2011, 10:55 PM
  2. precedence question
    By nimitzhunter in forum C Programming
    Replies: 4
    Last Post: 08-10-2010, 11:57 PM
  3. Precedence question
    By jw232 in forum C++ Programming
    Replies: 6
    Last Post: 06-04-2008, 01:23 AM
  4. question about declaration precedence
    By waltr in forum C Programming
    Replies: 4
    Last Post: 06-16-2006, 12:32 PM
  5. question about precedence (i think)
    By rootnix in forum C++ Programming
    Replies: 4
    Last Post: 09-25-2001, 11:30 AM

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