Illegal double increment operation???

Discussion in 'C Programming' started by news.pace.co.uk, Jan 16, 2007.

  1. Is the following legal C:

    count++++;

    Although I would expect to be shot for using such dodgy syntax, the question
    is, is it legal syntax?
    I was recently led to belive this should compile but I tried it with gcc and
    it failed. If it is not valid C syntax is it valid C++?
     
    news.pace.co.uk, Jan 16, 2007
    #1
    1. Advertising

  2. In article <eoikbr$bb$>,
    news.pace.co.uk <> wrote:
    >Is the following legal C:


    > count++++;


    >Although I would expect to be shot for using such dodgy syntax, the question
    >is, is it legal syntax?


    No.

    >I was recently led to belive this should compile but I tried it with gcc and
    >it failed. If it is not valid C syntax is it valid C++?


    It is an attempt to modify the same object twice between sequence
    points. But I'd have to check the references to find out whether
    it is a constraint violation or undefined behaviour.
    --
    Is there any thing whereof it may be said, See, this is new? It hath
    been already of old time, which was before us. -- Ecclesiastes
     
    Walter Roberson, Jan 16, 2007
    #2
    1. Advertising

  3. news.pace.co.uk

    Ben Pfaff Guest

    "news.pace.co.uk" <> writes:

    > Is the following legal C:
    >
    > count++++;
    >
    > Although I would expect to be shot for using such dodgy syntax, the question
    > is, is it legal syntax?


    Syntactically, yes it is legal. But semantically it violates a
    constraint: the operand of ++ must be a modifiable lvalue, but
    the result of ++ is not a modifiable lvalue. Thus, ++ can't be
    applied to its own result. If it weren't a constraint violation,
    it would be undefined behavior due to modifying an object twice
    between sequence points.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
     
    Ben Pfaff, Jan 16, 2007
    #3
  4. news.pace.co.uk

    Ben Pfaff Guest

    -cnrc.gc.ca (Walter Roberson) writes:

    > In article <eoikbr$bb$>,
    > news.pace.co.uk <> wrote:
    >>Is the following legal C:

    >
    >> count++++;

    >
    >>Although I would expect to be shot for using such dodgy syntax, the question
    >>is, is it legal syntax?

    >
    > No.


    There's nothing wrong with the syntax. It fits the
    "postfix-expression" syntax just fine:

    postfix-expression:
    primary-expression
    postfix-expression [ expression ]
    postfix-expression ( argument-expression-listopt )
    postfix-expression . identifier
    postfix-expression -> identifier
    postfix-expression ++
    postfix-expression --
    ( type-name ) { initializer-list }
    ( type-name ) { initializer-list , }

    >>I was recently led to belive this should compile but I tried it with gcc and
    >>it failed. If it is not valid C syntax is it valid C++?

    >
    > It is an attempt to modify the same object twice between sequence
    > points. But I'd have to check the references to find out whether
    > it is a constraint violation or undefined behaviour.


    It's a constraint violation. Constraints are not syntactical;
    otherwise they'd be formulated as part of the syntax and wouldn't
    need to be explicitly stated.
    --
    "When I have to rely on inadequacy, I prefer it to be my own."
    --Richard Heathfield
     
    Ben Pfaff, Jan 16, 2007
    #4
  5. "news.pace.co.uk" <> writes:
    > Is the following legal C:
    >
    > count++++;
    >
    > Although I would expect to be shot for using such dodgy syntax, the question
    > is, is it legal syntax?
    > I was recently led to belive this should compile but I tried it with gcc and
    > it failed.


    The *syntax* is legal (and equivalent to "count ++ ++"), but it's
    semantically invalid (a constraint violation requiring a diagnostic).
    The operand of the "++" operator must be an lvalue (an expression that
    denotes an object), but it doesn't yield an lvalue. "count" is an
    lvalue, so "count++" is ok, but "count++" is not an lvalue, so
    "count++++" is not.

    > If it is not valid C syntax is it valid C++?


    We don't know; try comp.lang.c++ (or try a conforming C++ compiler, or
    check a C++ reference).

    But if you'll think about what you're really trying to do, there's
    likely to be a more straightforward way to do it. If you want to increase the
    value of count by 2, just write:

    count += 2;

    (I think some programmers think of "++" as a magic bullet, and use it
    where it's really not required.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 16, 2007
    #5
  6. "news.pace.co.uk" wrote:
    >
    > Is the following legal C:
    >
    > count++++;
    >
    > Although I would expect to be shot for using such dodgy syntax,
    > the question is, is it legal syntax?
    > I was recently led to belive this should compile but I tried it
    > with gcc and it failed. If it is not valid C syntax is it valid
    > C++?


    Short answer:

    It's not legal because "count++" is not an l-value.

    In fact, my C compiler gives the error:

    '++' needs l-value

    What error does gcc give?

    I doubt it's legal in C++, but you would have to ask in c.l.c++,
    which is down the hall and to the left.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Jan 16, 2007
    #6
  7. news.pace.co.uk

    Eric Sosman Guest

    Walter Roberson wrote:
    > In article <eoikbr$bb$>,
    > news.pace.co.uk <> wrote:
    >> Is the following legal C:

    >
    >> count++++;

    >
    >> Although I would expect to be shot for using such dodgy syntax, the question
    >> is, is it legal syntax?

    >
    > No.


    Au contraire: Yes. The token stream is syntactically valid.
    It is semantically invalid, but that's another matter.

    >> I was recently led to belive this should compile but I tried it with gcc and
    >> it failed. If it is not valid C syntax is it valid C++?

    >
    > It is an attempt to modify the same object twice between sequence
    > points. But I'd have to check the references to find out whether
    > it is a constraint violation or undefined behaviour.


    Things never get that far. The constraint violation (for
    which a diagnostic is required) is that `count++' is not an
    lvalue, as required by the rightmost `++'.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jan 17, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,568
    Victor Bazarov
    Feb 17, 2005
  2. Replies:
    104
    Views:
    11,081
    Jordan Abel
    Oct 28, 2005
  3. Replies:
    99
    Views:
    2,533
    eliza81
    Jun 11, 2010
  4. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    914
    Alf P. Steinbach /Usenet
    May 22, 2011
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    568
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page