Thread: precedence of relational operators

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    50

    precedence of relational operators

    came across the foll code:


    Code:
    #include<stdio.h>
        
        main()
        {
           int i=4,j=7;
           
           j=j||(printf("you can")&&(i=5));
           printf("%d %d",i,j);
        }
    output: 4 1
    Athough I am specifying the braces for the && operator so that it gets executed first..Then also the value of i remains 4 only..Why doesnot it gets changed to 5??Also the printf doesnot execute??

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Adding parenthesis does not force the program to evaluate that code... the compiler can make a determination and do any number of things to optimize that line. True or anything is equal to true. In your case, it appears that when your compiler evaluates the above code, it can see that the expression will always evaluate to true because j is non-zero (true) so the part in the parenthesis is never executed/evaluated at all (as you stated, the printf call does not execute) and i remains 4.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    Quote Originally Posted by hk_mp5kpdw View Post
    Adding parenthesis does not force the program to evaluate that code... the compiler can make a determination and do any number of things to optimize that line. True or anything is equal to true. In your case, it appears that when your compiler evaluates the above code, it can see that the expression will always evaluate to true because j is non-zero (true) so the part in the parenthesis is never executed/evaluated at all (as you stated, the printf call does not execute) and i remains 4.
    Thank you for your explaination but as per the foll link it says && has higher precedence than ||..So why does in my case || gets evaluated first C Language Operator Precedence Chart

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Because you put the && inside the parentheses it is not seen because j is true. Since j is true and you use the OR evaluation the rest of the statement doesn't need to be evaluated.

    Jim

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    @jimblumberg thank you but even if I remove paranthesis then also the result is the same i.e j is considered first and then the other parts of the expression..As far as I know precedence means That operator will be considered first...then why doesnt in my case && is considered first and its operands like printf and j are considered first??

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Saurabh Mehta
    As far as I know precedence means That operator will be considered first
    You are mistaken: precedence determines grouping, not order of evaluation. It so happens that in some cases the order of evaluation follows logically from the grouping, but that does not mean that precedence determines order of evaluation.
    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

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    Quote Originally Posted by laserlight View Post
    You are mistaken: precedence determines grouping, not order of evaluation. It so happens that in some cases the order of evaluation follows logically from the grouping, but that does not mean that precedence determines order of evaluation.
    Thank you it was great explaination ...I have a last doubt that does compiler decides order of evalauation or is it always left to right??

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Normally the compiler decides, but in this case (see Matticus' link) it is left to right.
    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

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Post and Pre Increment
    Read this to see how precedence, evaluation order and side-effects can conspire to make a mess of your day.

    Cramming too much into one line is almost always more trouble than it's worth.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what' value relational operators returned
    By liyanhong in forum C Programming
    Replies: 10
    Last Post: 06-15-2010, 04:27 AM
  2. problem with relational operators
    By manoncampus in forum C++ Programming
    Replies: 10
    Last Post: 12-13-2005, 07:15 PM
  3. Relational Operators
    By Surfin_Bird in forum C Programming
    Replies: 9
    Last Post: 01-16-2005, 07:45 PM
  4. problem with relational operators
    By Sargnagel in forum C Programming
    Replies: 4
    Last Post: 10-13-2002, 09:45 AM
  5. Relational and Logical Operators
    By GSLR in forum C Programming
    Replies: 2
    Last Post: 03-03-2002, 04:33 PM

Tags for this Thread