Illegal double increment operation???

N

news.pace.co.uk

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

Walter Roberson

Is the following legal C:

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.
 
B

Ben Pfaff

news.pace.co.uk said:
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.
 
B

Ben Pfaff


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 , }
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.
 
K

Keith Thompson

news.pace.co.uk said:
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.)
 
K

Kenneth Brody

news.pace.co.uk said:
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:[email protected]>
 
E

Eric Sosman

Walter said:

Au contraire: Yes. The token stream is syntactically valid.
It is semantically invalid, but that's another matter.
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 `++'.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top