Thread: Order of operations question

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    3

    Order of operations question



    Code:
    #include <stdio.h>
    void main()
    {
    	float a = 4, b = 4, c = 8;
    	a = (a + c) / ++c;
    	printf("\n a = %f", a);
    
    }
    Does the () take precedence over the pre increment or not?
    In some text books the () appears higher up in the table of precedence.

    I ran this program in VS 2003 and I got 1.444, which is 13 / 9.

    Why don't I get (12) / 9 if the parens come first?

    Last edited by nicomp; 09-20-2006 at 09:40 AM.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    It does take higher precedence.

    But you have to consider the precedence of all operators involved along with their associativity rules. You have 4 operators in effect on that formula. (), +, / and ++.

    So... let's order them by precedence and associativity.

    1st ()
    2nd +
    3rd ++
    4th /

    The addition operator is moved up because the grouping operator forces evaluation of the expression contained inside. So, the first thing that happens is the sum of a with c.

    The pre-increment has higher precedence over the division. As such, it is evaluated first. c is incremented.

    Only then is the division made.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > I ran this program in VS 2003 and I got 1.444, which is 13 / 9.

    I didn't see this, sorry.

    That may come from the fact a, b and c are floats and some manner of rounding is happening. Remember that floats have a very low precision.

    Change b and c to int and a to double. the result will have a much higher precision which I believe will be acceptable for your problem
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    Quote Originally Posted by Mario F.
    So, the first thing that happens is the sum of a with c..
    I don't understand. If the first thing that happens is the sum of a with c then a+c should be 12. Instead, a+c is giving 13 which implies that c has been incremented already.

    What am I missing?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I suspect the answer is that it is undefined, though I am not entirely sure.
    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

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    If you are using the ++ operator before the variable name, the first thing that happens is the variable is incremented. Which is the exact opposite of using ++ after the variable name, it will increment it after the other operations are preformed.
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I've always used ++ after the variable and I have never had any problems with it.
    Now when I start thinking using ++ before the variable seems to be very useful in some cases...

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > Does the () take precedence over the pre increment or not?
    Do not confuse the fact that you have used () with "This is always done first".

    > In some text books the () appears higher up in the table of precedence.
    Sounds like some other books are wrong then.

    > I ran this program in VS 2003 and I got 1.444, which is 13 / 9.
    Good for you.
    Your expression is undefined, so any answer (or indeed no answer) is entirely plausable.
    http://c-faq.com/expr/index.html

    NEVER EVER use ++ on a variable, and attempt to use that same variable in another part of the expression. It's undefined - no if's, but's or maybe's.

    Also read the part of that FAQ which talks about "sequence points".

    Given
    f() + g() / h();
    No amount of () or operator precedence will tell the compiler which function to call first. Precedence and () only describe how sub-expressions are combined, not evaluated.
    It is perfectly valid for a compiler to call f() first.

    Oh yeah, the void main is wrong as well.
    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.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Imagine it as a series of function calls like this:
    Code:
    a = divide(add(a, c), increment(c));
    C++ doesn't specify which function argument will be evaluated first, so for the divide function, you don't know whether the add(a,c) will run first or the increment(c). The result is undefined.

    All the parentheses do is stop it from being this:
    Code:
    a = add(a, divide(c, increment(c)));
    Last edited by Daved; 09-20-2006 at 10:22 AM.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    Quote Originally Posted by Salem
    > Oh yeah, the void main is wrong as well.
    5275: Told ya!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Embedded SQL Order By
    By cjohnman in forum C Programming
    Replies: 12
    Last Post: 04-15-2008, 03:45 PM
  2. Numerical order with ifs question?
    By JDrake in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2006, 01:29 PM
  3. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  5. Question involving using math operations...
    By Screwz Luse in forum C Programming
    Replies: 6
    Last Post: 12-04-2001, 05:20 PM