Valid C syntax.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • paresh

    Valid C syntax.

    Is this the valid C statement.

    int a,b,c;
    c = 5;
    <<<
    a = b = c;
    >>>
    Can anyone throw the light on this.

    -Paresh
  • viza

    #2
    Re: Valid C syntax.

    Hi

    On Fri, 21 Nov 2008 03:05:21 -0800, paresh wrote:
    Is this the valid C statement.
    >
    int a,b,c;
    c = 5;
    <<<
    a = b = c;
    >>
    No. >>is a parse error.
    Can anyone throw the light on this.
    Is it the output of some kind of diff?

    viza

    Comment

    • Nick Keighley

      #3
      Re: Valid C syntax.

      On 21 Nov, 11:05, paresh <pareshvarsh... @gmail.comwrote :
      Is this the valid C statement.
      >
      int  a,b,c;
      c = 5;
      <<<
      a = b =  c;
      >
      Can anyone throw the light on this.
      it is unclear what you are asking. What is "this"?

      1. the declaration and two statements above are not "a valid
      statment".
      They are several statments.

      2. "<<<" is a syntax error

      3. "a = b = c;" is a valid statement. Why wouldn't it be?

      Note assignment ("=") evaluates right to left so the above
      assigns c to b giving a result equal to the new b which is
      then assigned to a.

      --
      Nick Keighley



      Comment

      • Ben Bacarisse

        #4
        Re: Valid C syntax.

        paresh <pareshvarshney @gmail.comwrite s:
        Is this the valid C statement.
        >
        int a,b,c;
        c = 5;
        <<<
        a = b = c;
        >>>>
        >
        Can anyone throw the light on this.
        Presuming that the "<<<" and ">>>" are just there to mark the bit you
        are asking about then, yes, it is valid. Both a and b are assigned
        the value of c.

        It is parsed as 'a = (b = c);'. The result of the right-hand b = c is
        the value that b will have after the assignment and it is that
        value that gets assigned to a (after any conversions). This matters
        most when the types of a, b and c differ. In fact, in such a case I
        would advise against an assignment like this since the effect can be
        mildly confusing.

        --
        Ben.

        Comment

        • paresh

          #5
          Re: Valid C syntax.

          yes "<<<" and ">>>" are for marking.

          I want to know that the "a = b = c;" statement is valid syntax
          according to ISO C.

          I ran it myself on various C/C++ compilers it worked. However some one
          said to me it is not valid C syntax as
          same variable cannot be l-value and r-value in same statement.

          -Paresh

          Comment

          • viza

            #6
            Re: Valid C syntax.

            On Fri, 21 Nov 2008 03:38:16 -0800, paresh wrote:
            int a,b,c;
            >
            I want to know that the "a = b = c;" statement is valid syntax according
            to ISO C.
            It is valid but usually stupid to use it.
            It assigns the value of c to both a and b.

            If the types are different the conversions become more complicated.

            viza

            Comment

            • Chris Dollin

              #7
              Re: Valid C syntax.

              paresh wrote:
              yes "<<<" and ">>>" are for marking.
              >
              I want to know that the "a = b = c;" statement is valid syntax
              according to ISO C.
              >
              I ran it myself on various C/C++ compilers it worked. However some one
              said to me it is not valid C syntax as
              same variable cannot be l-value and r-value in same statement.
              Well, let's note first that in `a = b = c`, all the variables are
              different, so even if the same variable couldn't be both an
              lvalue and rvalue in one statement, it wouldn't matter.

              Let's note next that if "some one" really did say that, they'd have
              forgotten about statements like `i = i + 1;` (or `i += 1;` or `i++;`).

              What I suspect they said, or thought they were saying, is that you're
              not allowed [1] to update a variable /more than once/ in a single statement
              (actually, before the next sequence point, but that's another discussion),
              and if you update a variable, you can only read it in that same statement
              (ditto) /for the purpose of determining its new value/.

              So `x = x = 0;` isn't allowed. More subtly, `a[i] = a[j] = 0;` isn't
              allowed if `i == j`. Also `i = i + i++;` isn't allowed.

              Finally, this isn't a matter of /syntax/ -- how to construct larger
              bits of program from smaller bits -- at all. It's a matter of /semantics/,
              the meaning of bits of programs. The statement `a[i] = a[j] = 0;` is
              syntactically fine. Given appropriate definitions for `a`, `i`, and `j`,
              its compile-time semantics is fine too -- no constraint violation.
              But to be meaningful when it's executed, `i` must not equal `j`, and
              this is not a matter of syntax.

              [1] On pain of undefined behaviour, ie, the program's behaviour becomes
              unpredictable.

              --
              "There's no 'we' in 'team'." Unsaid

              Hewlett-Packard Limited Cain Road, Bracknell, registered no:
              registered office: Berks RG12 1HN 690597 England

              Comment

              • Ben Bacarisse

                #8
                Re: Valid C syntax.

                paresh <pareshvarshney @gmail.comwrite s:
                yes "<<<" and ">>>" are for marking.
                >
                I want to know that the "a = b = c;" statement is valid syntax
                according to ISO C.
                It is valid syntax. What is more, the semantics are well-defined too.
                I ran it myself on various C/C++ compilers it worked. However some one
                said to me it is not valid C syntax as
                same variable cannot be l-value and r-value in same statement.
                First, if this were forbidden (it is not) it would not be classed as
                invalid syntax. Syntax (in C) is just about the "shape" of the code;
                what sort of things can go where. Second, you were told something the
                "same variable" and all yours are different so what was said has
                nothing to do with your example. Third, what this person said is
                obviously false, unless you think that C forbids:

                x = x + 1;

                in which x is used both as a lvalue and as an r-value (C does not use
                the term rvalue be we can reasonably assume what was meant).

                --
                Ben.

                Comment

                • Martien Verbruggen

                  #9
                  Re: Valid C syntax.

                  On Fri, 21 Nov 2008 12:13:09 GMT,
                  viza <tom.viza@gm-il.com.obviousc hange.invalidwr ote:
                  On Fri, 21 Nov 2008 03:38:16 -0800, paresh wrote:
                  >
                  >int a,b,c;
                  >>
                  >I want to know that the "a = b = c;" statement is valid syntax according
                  >to ISO C.
                  >
                  It is valid but usually stupid to use it.
                  Why is it stupid?

                  Martien
                  --
                  |
                  Martien Verbruggen | Begin at the beginning and go on till you
                  | come to the end; then stop.
                  |

                  Comment

                  • paresh

                    #10
                    Re: Valid C syntax.

                    Thanks Chirs for your explanation.

                    That means "a=b=c;" is valid syntactically and semantically.
                    However if execution of the statement is right to left then it seems
                    that it will do what we are expecting.
                    On the other way (i.e left to right) it will mess up the variable "a".

                    Is "right to left" or "left to right" execution depends on the
                    compiler or its a standard?

                    -Paresh


                    Comment

                    • paresh

                      #11
                      Re: Valid C syntax.

                      On Nov 21, 5:19 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
                      paresh <pareshvarsh... @gmail.comwrite s:
                      yes "<<<" and ">>>" are for marking.
                      >
                      I want to know that the "a = b = c;" statement is valid syntax
                      according to ISO C.
                      >
                      It is valid syntax.  What is more, the semantics are well-defined too.
                      >
                      I ran it myself on various C/C++ compilers it worked. However some one
                      said to me it is not valid C syntax as
                      same variable cannot be l-value and r-value in same statement.
                      >
                      First, if this were forbidden (it is not) it would not be classed as
                      invalid syntax.  Syntax (in C) is just about the "shape" of the code;
                      what sort of things can go where.  Second, you were told something the
                      "same variable" and all yours are different so what was said has
                      nothing to do with your example.  Third, what this person said is
                      obviously false, unless you think that C forbids:
                      >
                        x = x + 1;
                      >
                      in which x is used both as a lvalue and as an r-value (C does not use
                      the term rvalue be we can reasonably assume what was meant).
                      >
                      --
                      Ben.
                      So what is the term used for rvalue in C?
                      Dont you think "b" is behaving as a lvalue and rvalue(whatever it is
                      called).

                      -Paresh

                      Comment

                      • James Kuyper

                        #12
                        Re: Valid C syntax.

                        paresh wrote:
                        yes "<<<" and ">>>" are for marking.
                        >
                        I want to know that the "a = b = c;" statement is valid syntax
                        according to ISO C.
                        >
                        I ran it myself on various C/C++ compilers it worked. However some one
                        said to me it is not valid C syntax as
                        same variable cannot be l-value and r-value in same statement.
                        Note: The C standard uses "lvalue" and "value", rather than "l-value"
                        and "r-value".

                        I think the person who told you that may have parsed this statement
                        incorrectly. I suspect he's thinking about it as if it was the
                        combination of a=b and b=c in a single statement. There is a rule which
                        says that, the behavior is undefined if, between two sequence points, a
                        value is stored in an object, and the stored value of that object is
                        retrieved for any purpose other than determining what the new value of
                        that object will be. Therefore, if you combined a=b with b=c in a single
                        statement without an intervening sequence point, the behavior would be
                        undefined. Example:

                        (a=b)*(b=c); // Behavior is undefined.

                        However, in reality, while a=b=c contains no sequence points, it is not
                        a combination of a=b with b=c. Instead, it is equivalent to

                        a = (b=c);

                        Every assignment expression ( =, +=, -=, *=, etc.) has a value (an
                        r-value, in your terms) which is the same as the value of it's left
                        operand after the assignment. Note that the value of an assignment can
                        be used immediately, even though the actual change to the stored value
                        of it's left operand doesn't have to happen until just before the next
                        sequence point. This means that the value of an assignment expression
                        maybe used even in circumstances, such as this one, where retrieve that
                        stored value would have undefined behavior.

                        Comment

                        • James Kuyper

                          #13
                          Re: Valid C syntax.

                          paresh wrote:
                          ....
                          So what is the term used for rvalue in C?
                          There is exactly one use of the term "rvalue" in the C standard. It
                          occurs in footnote 53, in section 6.3.2.1p1. It says: "What is sometimes
                          called ‘‘rvalue’’ is in this International Standard described as the
                          ‘‘value of an expression’’."
                          Dont you think "b" is behaving as a lvalue and rvalue(whatever it is
                          called).
                          No. The rvalue is (b=c), not b itself.

                          Comment

                          • blargg

                            #14
                            Re: Valid C syntax.

                            paresh wrote:
                            [...]
                            That means "a=b=c;" is valid syntactically and semantically.
                            However if execution of the statement is right to left then it seems
                            that it will do what we are expecting.
                            On the other way (i.e left to right) it will mess up the variable "a".
                            [...]
                            Yes, if we executed

                            (a = b) = c;

                            we'd have undefined behavior, because we are attempting to modify 'a' more
                            than once between two sequence points.

                            Comment

                            • Ben Pfaff

                              #15
                              Re: Valid C syntax.

                              blargg.h4g@gish puppy.com (blargg) writes:
                              Yes, if we executed
                              >
                              (a = b) = c;
                              >
                              we'd have undefined behavior, because we are attempting to modify 'a' more
                              than once between two sequence points.
                              Such a statement is a constraint violation that requires a
                              diagnostic, because the result of a assignment operator is not an
                              lvalue.
                              --
                              "When in doubt, treat ``feature'' as a pejorative.
                              (Think of a hundred-bladed Swiss army knife.)"
                              --Kernighan and Plauger, _Software Tools_

                              Comment

                              Working...