a+++b

This is a discussion on a+++b within the C Programming forums, part of the General Programming Boards category; Hello all, This is probably a trivially easy question, but here goes anyway: How or why do C compilers know ...

  1. #1
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99

    a+++b

    Hello all,

    This is probably a trivially easy question, but here goes anyway:

    How or why do C compilers know that the expression a+++b evaluates as (a++)+ b?

    In the expression a+(++b), the prefix increment operator has a higher precedence than the binary + operator, which suggests to me that the parentheses are unnecessary and that a+(++b) too could be rewritten as a+++b. Also, associativity would appear to me to be irrelevant here, since we are dealing with operators of different precedence, which in itself should determine the sequence in which the operations are carried out.

    Clearly I am missing something here but I have no idea what. I have been right through K&R and can't find any help there.

    Sorry if this seems like a stupid question; I have only been learning C for a few weeks.

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    You will not find stuff like that in K&R because it is undefined behavior according to the C standard.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    So does that mean that to comply with the standard you must use parentheses?

    These gentlemen don't appear to think there is anything wrong with the expression without parentheses:

    http://publications.gbdirect.co.uk/c...rithmetic.html


    "Because the unary operators have very high precedence, you can work out what they do before worrying about the other operators. One thing to watch out for is the way that ++ and -- can be before or after their operands; the expression

    a + -b++ + c

    has two unary operators applied to b. The unary operators all associate right to left, so although the - comes first when you read the expression, it really parenthesizes (for clarity) like this:

    a + -(b++) + c"

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    If you use more than one number and perform operations such as -, ++, -- with or without parenthesises, the result is undefined. The end.
    The problem isn't about which operators goes first, but because the compiler is not required to finish the line is linear order.
    If it looks like a b c, then the compiler can finish c first, then a and then b. But some other compiler may do it a, b, c. So the result is undefined.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Crappy code = crappy results.

  6. #6
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    Quote Originally Posted by Elysia View Post
    If you use more than one number and perform operations such as -, ++, -- with or without parenthesises, the result is undefined. The end.
    The problem isn't about which operators goes first, but because the compiler is not required to finish the line is linear order.
    If it looks like a b c, then the compiler can finish c first, then a and then b. But some other compiler may do it a, b, c. So the result is undefined.
    I get your point, but in my example a+++b there are only two operands, and the question is surely not "what order should be operators be executed in?", but "what are the operators?"

    Anyhow, if the answer is simply that this is crappy / non-standard code and therefore there is no definite answer to the question, I can accept that.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Quote Originally Posted by DL1 View Post
    Anyhow, if the answer is simply that this is crappy / non-standard code and therefore there is no definite answer to the question, I can accept that.
    The problem isn't the order of the operators. It's the order of the number and in what order the compiler assembles them.
    And besides, in your example, is the code
    (a++)+b or a+(++b)? How is the compiler supposed to know? The compiler doesn't necessarily execute the the result from left to right.

    So the result can be (a = 10, b = 20), 10 + 20 or 10 + 21. There's no way to tell.
    Last edited by Elysia; 06-27-2008 at 08:43 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by Elysia View Post
    If you use more than one number and perform operations such as -, ++, -- with or without parenthesises, the result is undefined. The end.
    The problem isn't about which operators goes first, but because the compiler is not required to finish the line is linear order.
    If it looks like a b c, then the compiler can finish c first, then a and then b. But some other compiler may do it a, b, c. So the result is undefined.
    Are you actually sure that these expressions here are undefined? Otherwise you might just as well claim that the following idiomatic expression is undefined.
    Code:
    while ((*a++ = *b++));
    If I'm not mistaken the undefinedness comes to play when you apply side-effects to the same variable more than once within sequence points.

    I would definately use brackets for something like a+++b, though.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    This might not be undefined:
    (a++)+b
    Or
    a+(++b)

    But I'm almost certain that...
    a+++b
    ...is undefined, since the compiler can evaluate the expressions in any order.

    (Or on second thought, the whole darn thing is mind-boggling, so I can't be sure. I haven't used any expression other than a++ = b++ or a = b++ or a++ = b.)
    Last edited by Elysia; 06-27-2008 at 08:59 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  10. #10
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    This strikes me as a deficiency in the language then: We have an expression that in itself is (or at least may be) meaningful and evaluable but is possibly uncompilable simply because the C tokens are themselves not distinguishable. If the increment operator were denoted by, say, rather than ++, then you would have either a+b or a+b and no ambiguity.

    This is rather disillusioning...

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Quote Originally Posted by DL1 View Post
    If the increment operator were denoted by, say, rather than ++, then you would have either a+b or a+b and no ambiguity.
    That is not the problem.
    If all compilers read the code from left to right, it would become
    (a++)+b
    And if from right, it would be
    a+(++b)

    Actually, the problem, YOUR problem is that you don't use spaces:
    a++ + b
    a + ++b

    No ambiguity there.
    And the problem lies in the order of evaluation, as explained above.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    At best it is undefined how the compiler parses that expression. (This has nothing to do with the undefinedness of i = ++i). You might also be able to remove the ambiguity by introducing whitespace: a++ +b vs a+ ++b.

    There are other interesting parsing cases, e.g a++b (doesn't compile) vs a+ +b.
    Last edited by anon; 06-27-2008 at 09:25 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Even if the compiler could read it properly, I'm sure Humans couldn't.
    If it was even like this I could understand it (although it would be easy to miss-read if I didn't have my coffee yet):
    Code:
    a + ++b or a++ + b
    There's a reason spaces were invented.

  14. #14
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Order of operation. As the parser reads the equation it sees

    a ok we are operating on the variable a
    a++ ok we are incrementing the variable a after this operation

    +b ok we are adign b to the equation.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  15. #15
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    But surely the compiler doesn't recogize white space?!

    And I don't see that this is an problem of order of evaluation.

    If the code were unambiguous, it wouldn't matter what order the terms were evaluated in, since they don't affect each other.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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