470,863 Members | 1,376 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,863 developers. It's quick & easy.

Illegal double increment operation???

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++?

Jan 16 '07 #1
6 6497
In article <eo*********@nh.pace.co.uk>,
news.pace.co.uk <st********@pace.co.ukwrote:
>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
Jan 16 '07 #2
"news.pace.co.uk" <st********@pace.co.ukwrites:
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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}
Jan 16 '07 #3
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <eo*********@nh.pace.co.uk>,
news.pace.co.uk <st********@pace.co.ukwrote:
>>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
Jan 16 '07 #4
"news.pace.co.uk" <st********@pace.co.ukwrites:
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) ks***@mib.org <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.
Jan 16 '07 #5
"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:Th*************@gmail.com>

Jan 17 '07 #6
Walter Roberson wrote:
In article <eo*********@nh.pace.co.uk>,
news.pace.co.uk <st********@pace.co.ukwrote:
>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
es*****@acm-dot-org.invalid
Jan 17 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Chris Yates | last post: by
6 posts views Thread by James Thurley | last post: by
13 posts views Thread by Shirsoft | last post: by
13 posts views Thread by jehugaleahsa | last post: by
8 posts views Thread by bintom | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.