Thread: precedence strange thing

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

    precedence strange thing

    OK guys, I got this code-lines:

    PHP Code:
    int x 1;
    int z = ++* ++* ++* ++x
    According to my understanding, ++ has a higher precedence than *, thus it will first increment x 4 times and later do the multiplying.
    However, what happens is that it increments the two first x's, later multiplies them, then it goes to the next x, increments it, and multiplies it with the result before and so on.

    Am I missing something?
    Trying to find something about it at the K&R book, but have no idea.

    Thanks.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    It has nothing to do with operator precedence. This is classic undefined behavior due to multiple references to a variable with an pre-increment operator. Only one such reference can occur between two sequence points.

    In other words the line of code you wrote is crazy and meaningless.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    7
    OK, I had the feeling, you will say that.
    Then, how can I know when to apply the precedence laws and when I know it is meaningless.

    Another question - is this code-line also a meaningless?
    PHP Code:
    int a 1;
    int b = (a=1) + (a=9); 
    b can be 2 or 18, it depends on the compiler, right?

    Thanks in adv.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Expressions
    Really get to understand things like "modify once" and "sequence points" and such-like.

    Nobody writes code like that in real life - it only seems to be idiot tutors who think it shows something deep and meaningful (whereas all it shows is how shallow their knowledge of C really is).
    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.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    7
    Thanks Salem,

    So if I understood correctly, the (++x) * (++x) * (++x) is creepy code cause it doesn't have a clear sequence point, and it depends on the compiler which one is evaluated first.
    It can increment x 3 times, and then multiply or it can increment 2 times and multiply and so on.
    But when I have ++x + 1, then it first increments x then it add 1 to the result (due to precedence law). Am I right?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    ++x + 1 is well defined, so long as there isn't another x in the same expression.
    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. About precedence of && and ||
    By noobiept in forum C Programming
    Replies: 11
    Last Post: 09-12-2010, 06:38 PM
  2. A very strange thing
    By gustavosserra in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2003, 12:43 PM
  3. Strange thing happens...
    By funkydude9 in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2002, 07:56 PM
  4. a strange thing
    By SuperNewbie in forum C# Programming
    Replies: 2
    Last Post: 07-14-2002, 10:06 AM