Thread: operator precedence

  1. #1
    Charming Registered User
    Join Date
    May 2011
    Posts
    49

    operator precedence

    Hello everyone, my first post in your forum and from my questions you can assume correctly that i am under novice level.
    I am trying to understand the short-circuit behavior of logical expressions so I made the following programm:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i, j, k;
    	
    	i = 3; j = 4; k = 5;
    	printf("variables are: i = %d, j = %d, k = %d\n", i, j, k);
    	printf("output of the expression i < j || ++j < k is: %d\n", i < j || ++j < k);
    	printf("variables are: i = %d, j = %d, k = %d\n", i, j, k);
    	
    	return 0;
    }
    According of how I understand the operator precedence, the following should happen:
    1. ++j will increase to 5.
    2. i < j and j < k will evaluate next, resulting in 1 and 0 (I assume that in both expressions the j has the value of 5)
    3. 1 || 0 will evaluate last, resulting in 1.

    First of all I would like to ask if my thinking is correct.

    Next something strange appear to the output:
    variables are: i = 3, j = 4, k = 5
    output of the expression i < j || ++j < k is: 1
    variables are: i = 3, j = 4, k = 5
    I was expecting j = 5. In fact if i modify the expression and in place of the logical OR I put a logical AND "i < j && ++j < k" the output seems fine:
    variables are: i = 3, j = 4, k = 5
    output of the expression i < j && ++j < k is: 0
    variables are: i = 3, j = 5, k = 5
    Can someone help me understand why this occur?
    I am using gcc compiler in windows 7 64bit in case this information helps.
    I appreciate your time to read this.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Read this post of mine on another forum to understand that trying to understand complex expressions with multiple side effects is a waste of time.

    You never see anything like that in professional code, so you never have to deal with it "for real".
    It's only moronic tutors who ever ask you this kind of question.
    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.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's a personal opinion, but I don't think logical short-circuit is that complex or that academic and you will certainly see it "for real".

    What is logical short-circuit? It means that when you use the logical operators || or &&, C will always go left-to-right and stop once it knows the end result. So in this case, i < j is evaluated first. Since it is true, the || is true, and therefore the right side never happens -- more specifically, ++j never happens. Hence j keeps its original value.

  4. #4
    Charming Registered User
    Join Date
    May 2011
    Posts
    49
    I am far away from write professional code, I am just trying to understand the basics. It is more to satisfy my curiosity of how things really work than the fact that I will ever use something like that in my code !!!
    But thank you for your quick answer Salem and I am on my way to read your post.

  5. #5
    Charming Registered User
    Join Date
    May 2011
    Posts
    49
    @tabstop Thanks for your answer but if I may take my question one level deeper, then why the logical OR and AND operators appear as of lower precedence than the ++(prefix) and relational < operators in every table about operator precedence I have looked in the web. Either the tables should update or my thinking has flaws.
    Apologize if I am becoming too annoying with these kind of questions.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because they are of lower precedence. Notice how the < happened before the || happened. Order of precedence and left-to-right association aren't really the same thing, and || and && are nearly the only operators that specify a "stopping point" between the two halves. (In other words: if I write (1*3) + (4-7), the + operator doesn't care in which order the two halves happen, only that they both happen first before the results are added together. But || does care.)

  7. #7
    Charming Registered User
    Join Date
    May 2011
    Posts
    49
    @tabstop. So in my case i < j evaluate to 1 because of precedence and then the || "sees" that in his left branch has the value 1 because of left-to-right association and stops there evaluating the whole expression to 1. "Stopping point" was the key Your answer put my synapses at ease. Thank you very much for your time to help me!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operator Precedence Puzzle
    By sawer in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2010, 01:21 AM
  2. Operator Precedence Problem
    By bthomson900 in forum C Programming
    Replies: 3
    Last Post: 11-10-2010, 11:37 PM
  3. Operator overloading and precedence
    By Sharke in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2009, 11:15 PM
  4. Operator Precedence?
    By cpjust in forum C++ Programming
    Replies: 53
    Last Post: 05-04-2008, 06:43 PM
  5. Operator Precedence
    By alex1067 in forum C Programming
    Replies: 7
    Last Post: 03-17-2008, 01:38 AM