Thread: A C Puzzler...

  1. #16
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Magos.

    Sure, but saying, "everything", is such a broad generalisation.

    Operator associativity is well defined, but yes,
    some operators do evaluate from right to left.

    Here are some examples that have right associativity.

    = The assignment operator.
    ++ Postfix increment operator.
    ++ Prefix increment operator.

    There are quite a few more but I'll leave those to the inquisitive.
    R.I.P C89

  2. #17
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    All their programs contain semi-colons and all of them print the wrong string.

    I think you have to be more specific about the rules.

  3. #18
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    No, you just have to be less pedantic.

    Who cares what the 8 translation phases of the c preprocessor do or what the compiler and linker add to the source code.

    It was pretty straight forward, you're just another instance of the bollocks I have to contend with on this forum.
    R.I.P C89

  4. #19
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Then isn't my version valid?

    ..., you're just another instance of the bollocks I have to contend with on this forum.
    why do you come here then?
    Last edited by major_blagger; 02-27-2004 at 09:17 AM.

  5. #20
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Originally posted by major_blagger
    why do you come here then?
    To learn and share.
    R.I.P C89

  6. #21
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Magos if you re-read I made sure to state that expressions were evaluated from left to right. Your example is not an expression.

  7. #22
    Registered User
    Join Date
    May 2003
    Posts
    161
    Magos if you re-read I made sure to state that expressions were evaluated from left to right. Your example is not an expression.
    Assignments are expressions-- they return the assigned value. If they didn't, you wouldn't be able to do things like a=b=c=3 in C.

  8. #23
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    I thought the order of which the comma operator was evaluated was not defined in the standart, and thus, well, not standard?
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  9. #24
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    From the standard
    6.5.17 Comma operator
    Syntax
    1 expression:
    assignment-expression
    expression , assignment-expression
    Semantics
    2 The left operand of a comma operator is evaluated as a void expression; there is a
    sequence point after its evaluation. Then the right operand is evaluated; the result has its
    type and value.
    If an attempt is made to modify the result of a comma operator or to
    access it after the next sequence point, the behavior is undefined.
    3 EXAMPLE As indicated by the syntax, the comma operator (as described in this subclause) cannot
    appear in contexts where a comma is used to separate items in a list (such as arguments to functions or lists
    of initializers). On the other hand, it can be used within a parenthesized expression or within the second
    expression of a conditional operator in such contexts. In the function call
    f(a, (t=3, t+2), c)
    the function has three arguments, the second of which has the value 5.
    The one who says it cannot be done should never interrupt the one who is doing it.

  10. #25
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Note: The following is 100% personal opinion.
    I personally don't call x = y = z = 5; expressions to avoid confusions with conditional expressions. To me expressions have to be simplified down and not assigned a value. Such as 3x -5 + 2x is simplified down to 5x - 5 (or 5(x-1) ) y == x + 5 is simplified down to true or false.

    x = 5 I call assignment because that is what its doing. Now if you have x = y = z = 5 == 10; I would call the 5 == 10 an expression and the rest assignments.

    But to avoid confusion from now on I'll refer to them as coniditional expressions.

Popular pages Recent additions subscribe to a feed