?: as an lvalue

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

    ?: as an lvalue

    Hi Everyone,

    I have the following piece of code, and i expected an error, however
    i don't get an error,

    int main()
    {
    int aa=0,b=0;
    1>0?aa:b = 10;
    printf("value %d %d\n",aa,b);
    return(0);
    }

    and output is value 0 0

    Thanks in advance ! ! !
  • Kenny McCormack

    #2
    Re: ?: as an lvalue

    In article <f840856d-216c-4bab-ac4a-f962f88a2c3a@h1 1g2000prf.googl egroups.com>,
    Rahul <sam_cit@yahoo. co.inwrote:
    >Hi Everyone,
    >
    I have the following piece of code, and i expected an error, however
    >i don't get an error,
    >
    >int main()
    >{
    > int aa=0,b=0;
    > 1>0?aa:b = 10;
    printf("value %d %d\n",aa,b);
    return(0);
    >}
    >
    >and output is value 0 0
    >
    >Thanks in advance ! ! !
    int main(void) is better
    failure to include stdio.h
    unnecessarily parenthesizing the return value
    failure to indent the return statement properly
    meaningless variable names
    failure to capitalize "i" in text (e.g., "i don't get an error")
    excessive use of exclamation points
    improper capitalization of word "everyone"
    use of tacky expression "Thanks in advance"

    Comment

    • Richard Heathfield

      #3
      Re: ?: as an lvalue

      Rahul said:
      Hi Everyone,
      >
      I have the following piece of code, and i expected an error, however
      i don't get an error,
      >
      int main()
      {
      int aa=0,b=0;
      1>0?aa:b = 10;
      This is a syntax error (which therefore requires the implementation to
      diagnose it as such). The syntax of the conditional operator is:

      conditional-expression:
      logical-OR-expression
      logical-OR-expression ? expression : conditional-expression

      which your code violates.
      printf("value %d %d\n",aa,b);
      return(0);
      }
      >
      and output is value 0 0
      >
      Thanks in advance ! ! !
      Turn up your warning level. Which implementation are you using?

      --
      Richard Heathfield <http://www.cpax.org.uk >
      Email: -http://www. +rjh@
      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
      "Usenet is a strange place" - dmr 29 July 1999

      Comment

      • Rahul

        #4
        Re: ?: as an lvalue

        On Mar 30, 5:19 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
        Rahul said:
        >
        Hi Everyone,
        >
        I have the following piece of code, and i expected an error, however
        i don't get an error,
        >
        int main()
        {
        int aa=0,b=0;
        1>0?aa:b = 10;
        >
        This is a syntax error (which therefore requires the implementation to
        diagnose it as such). The syntax of the conditional operator is:
        >
        conditional-expression:
        logical-OR-expression
        logical-OR-expression ? expression : conditional-expression
        >
        which your code violates.
        >
        printf("value %d %d\n",aa,b);
        return(0);
        }
        >
        and output is value 0 0
        >
        Thanks in advance ! ! !
        >
        Turn up your warning level. Which implementation are you using?
        >
        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999
        I tried MS VC++ 8.0...

        Comment

        • Flash Gordon

          #5
          Re: ?: as an lvalue

          Rahul wrote, On 30/03/08 12:05:
          Hi Everyone,
          >
          I have the following piece of code, and i expected an error, however
          i don't get an error,
          In that case you need to tell your compiler to act as a C compiler
          rather than as an extended-C-like-language compiler.
          int main()
          {
          int aa=0,b=0;
          1>0?aa:b = 10;
          This is illegal and a C compiler is required to produce a diagnostic
          (warning, error or whatever).
          printf("value %d %d\n",aa,b);
          Not related to your current problem, but you need to include stdio.h to
          use printf.
          return(0);
          }
          >
          and output is value 0 0
          If you want to do this then a legal way is as follows:
          #include <stdio.h>

          int main()
          {
          int aa=0,b=0;
          *(1>0?&aa:&b) = 10;
          printf("value %d %d\n",aa,b);
          return(0);
          }
          --
          Flash Gordon

          Comment

          • Richard Heathfield

            #6
            Re: ?: as an lvalue

            Rahul said:
            On Mar 30, 5:19 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
            >Rahul said:
            >>
            Hi Everyone,
            >>
            I have the following piece of code, and i expected an error, however
            i don't get an error,
            >>
            int main()
            {
            int aa=0,b=0;
            1>0?aa:b = 10;
            >>
            >This is a syntax error (which therefore requires the implementation to
            >diagnose it as such). The syntax of the conditional operator is:
            >>
            > conditional-expression:
            > logical-OR-expression
            > logical-OR-expression ? expression : conditional-expression
            >>
            >which your code violates.
            >>
            printf("value %d %d\n",aa,b);
            return(0);
            }
            >>
            and output is value 0 0
            >>
            Thanks in advance ! ! !
            >>
            >Turn up your warning level. Which implementation are you using?
            >
            I tried MS VC++ 8.0...
            I think the switches you'll need for Visual C are -W4 -Za

            (-W4 turns the warning level up as far as it'll go - unless they've added
            another level in the last few years - and -Za switches off Microsoft
            extensions.)

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • Andrey Tarasevich

              #7
              Re: ?: as an lvalue

              Rahul wrote:
              I tried MS VC++ 8.0...
              I tried the same compiler and got essentially the same error, saying that left
              operand must be a lvalue.
              If you tried it at this link, then most likely your code was compiled as C++. In
              C++ it would be parsed as '1 0 ? aa : (b = 10)'. Additionally in C++ the
              result of ?: can be lvalue. It is not surprise that you didn't get an error.

              --
              Best regards,
              Andrey Tarasevich

              Comment

              • Richard Heathfield

                #8
                Re: ?: as an lvalue

                Flash Gordon said:
                Rahul wrote, On 30/03/08 12:05:
                >Hi Everyone,
                >>
                > I have the following piece of code, and i expected an error, however
                >i don't get an error,
                >
                In that case you need to tell your compiler to act as a C compiler
                rather than as an extended-C-like-language compiler.
                The OP is using http://www.dinkumware.com/exam/default.aspx to test his
                code. When I present that site with the following source:

                #include <stdio.h>

                int main(void)
                {
                int a = 0;
                int b = 0;

                1 0 ? a : b = 10;

                printf("%d %d\n", a, b);

                return 0;
                }

                and select the only C option I can find, which is the EDG C99 option, I get
                no diagnostic messages whatsoever (unless you count "Code compiled
                successfully!" as a diagnostic message).

                Either this is a bug in EDG's compiler, or the rules changed in C99. I can
                find no evidence of a rule change in C99.

                --
                Richard Heathfield <http://www.cpax.org.uk >
                Email: -http://www. +rjh@
                Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                "Usenet is a strange place" - dmr 29 July 1999

                Comment

                • Dave Hansen

                  #9
                  Re: ?: as an lvalue

                  On Mar 30, 7:19 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                  Rahul said:
                  >
                  Hi Everyone,
                  >
                  I have the following piece of code, and i expected an error, however
                  i don't get an error,
                  >
                  int main()
                  {
                  int aa=0,b=0;
                  1>0?aa:b = 10;
                  >
                  This is a syntax error (which therefore requires the implementation to
                  diagnose it as such). The syntax of the conditional operator is:
                  >
                  I believe the syntax is legal. It just doesn't do what the OP wanted
                  it to.

                  Consider that it parses as

                  (1>0)?(aa):(b=1 0);

                  Change it to 1<0?aa:b=10; and see the result...

                  Regards,

                  -=Dave

                  Comment

                  • Eric Sosman

                    #10
                    Re: ?: as an lvalue

                    Dave Hansen wrote:
                    On Mar 30, 7:19 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                    >Rahul said:
                    >>
                    >>Hi Everyone,
                    >> I have the following piece of code, and i expected an error, however
                    >>i don't get an error,
                    >>int main()
                    >>{
                    >>int aa=0,b=0;
                    >>1>0?aa:b = 10;
                    >This is a syntax error (which therefore requires the implementation to
                    >diagnose it as such). The syntax of the conditional operator is:
                    >>
                    >
                    I believe the syntax is legal. It just doesn't do what the OP wanted
                    it to.
                    >
                    Consider that it parses as
                    >
                    (1>0)?(aa):(b=1 0);
                    Would you care to place a small wager on that?

                    You could refer to the formal grammar in the Standard
                    to settle the question, or you could ask informally which
                    of = and ?: "binds more tightly." The parse you suggest
                    would follow if = binds more tightly, in which case

                    x = a ? b : c;

                    would parse as

                    (x = a) ? b : c;

                    Since we know that it actually parses as

                    x = (a ? b : c);

                    you may be about to lose some money ...

                    --
                    Eric.Sosman@sun .com

                    Comment

                    • Andrey Tarasevich

                      #11
                      Re: ?: as an lvalue

                      Richard wrote:
                      ...
                      But (c?x:y)=v;
                      I dont really know what to say.
                      Is there any reason why you believe that the property of "being an
                      lvalue" should be necessarily lost in the process of selection from two
                      lvalues of the same type?

                      I mean I'm OK personally with the way it works in C. I just like to know
                      what is it exactly in '(c?x:y)=v' that triggers a "I don't really know
                      what to say" reaction from some people.

                      --
                      Best regards,
                      Andrey Tarasevich

                      Comment

                      • Richard

                        #12
                        Re: ?: as an lvalue

                        Andrey Tarasevich <andreytarasevi ch@hotmail.comw rites:
                        Richard wrote:
                        >...
                        >But (c?x:y)=v;
                        >I dont really know what to say.
                        >
                        Is there any reason why you believe that the property of "being an
                        lvalue" should be necessarily lost in the process of selection from
                        two lvalues of the same type?
                        >
                        I mean I'm OK personally with the way it works in C. I just like to
                        know what is it exactly in '(c?x:y)=v' that triggers a "I don't really
                        know what to say" reaction from some people.
                        because its not a macro? it returns a value. I dont know the legalise
                        words but it seems "obvious" enough to me, but again it might be because
                        I am tainted.

                        Comment

                        • Willem

                          #13
                          Re: ?: as an lvalue

                          Eric wrote:
                          ) Dave Hansen wrote:
                          )Consider that it parses as
                          )>
                          ) (1>0)?(aa):(b=1 0);
                          )
                          ) Would you care to place a small wager on that?
                          )
                          ) You could refer to the formal grammar in the Standard
                          ) to settle the question, or you could ask informally which
                          ) of = and ?: "binds more tightly." The parse you suggest
                          ) would follow if = binds more tightly, in which case

                          Technically, it would also follow if = binds equally tightly.
                          Left-to-right and all that.

                          ) x = a ? b : c;
                          )
                          ) would parse as
                          )
                          ) (x = a) ? b : c;

                          Again, purely technically, it would not if = were to bind equally tightly.

                          ) Since we know that it actually parses as
                          )
                          ) x = (a ? b : c);
                          )
                          ) you may be about to lose some money ...

                          I'm not going to bet on it though.


                          SaSW, Willem
                          --
                          Disclaimer: I am in no way responsible for any of the statements
                          made in the above text. For all I know I might be
                          drugged or something..
                          No I'm not paranoid. You all think I'm paranoid, don't you !
                          #EOT

                          Comment

                          • Willem

                            #14
                            Re: ?: as an lvalue

                            Andrey wrote:
                            ) Richard wrote:
                            ) ...
                            )But (c?x:y)=v;
                            )I dont really know what to say.
                            )
                            ) Is there any reason why you believe that the property of "being an
                            ) lvalue" should be necessarily lost in the process of selection from two
                            ) lvalues of the same type?
                            )
                            ) I mean I'm OK personally with the way it works in C. I just like to know
                            ) what is it exactly in '(c?x:y)=v' that triggers a "I don't really know
                            ) what to say" reaction from some people.

                            Well then why not also make it possible for functions (that return
                            pointers) to be lvalues ?

                            returnspointert ostruct(foo)->bar = baz;

                            Or is that already legal ?


                            SaSW, Willem
                            --
                            Disclaimer: I am in no way responsible for any of the statements
                            made in the above text. For all I know I might be
                            drugged or something..
                            No I'm not paranoid. You all think I'm paranoid, don't you !
                            #EOT

                            Comment

                            • Andrey Tarasevich

                              #15
                              Re: ?: as an lvalue

                              Richard wrote:
                              >...
                              >I mean I'm OK personally with the way it works in C. I just like to
                              >know what is it exactly in '(c?x:y)=v' that triggers a "I don't really
                              >know what to say" reaction from some people.
                              >
                              because its not a macro? it returns a value. I dont know the legalise
                              words but it seems "obvious" enough to me, but again it might be because
                              I am tainted.
                              Well, unary '*' operator is also not a macro. Yet it evaluates to an
                              lvalue. Same for '[]' operator (by definition). Do you find this strange
                              as well?

                              --
                              Best regards,
                              Andrey Tarasevich

                              Comment

                              Working...