Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Illegal double increment operation???

Reply
Thread Tools

Illegal double increment operation???

 
 
news.pace.co.uk
Guest
Posts: n/a
 
      01-16-2007
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++?



 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      01-16-2007
In article <eoikbr$bb$(E-Mail Removed)>,
news.pace.co.uk <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      01-16-2007
"news.pace.co.uk" <(E-Mail Removed)> 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[]="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;}
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      01-16-2007
http://www.velocityreviews.com/forums/(E-Mail Removed)-cnrc.gc.ca (Walter Roberson) writes:

> In article <eoikbr$bb$(E-Mail Removed)>,
> news.pace.co.uk <(E-Mail Removed)> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-16-2007
"news.pace.co.uk" <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      01-16-2007
"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: <(E-Mail Removed)>

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-17-2007
Walter Roberson wrote:
> In article <eoikbr$bb$(E-Mail Removed)>,
> news.pace.co.uk <(E-Mail Removed)> 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
(E-Mail Removed)lid
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Post increment ++ has higher precedence than pre increment ++. Why? Alf P. Steinbach /Usenet C++ 0 05-22-2011 12:03 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C++ 99 06-11-2010 12:51 PM
post increment or pre increment? Peng Yu Perl Misc 7 11-23-2008 11:44 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C Programming 104 10-27-2005 11:44 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM



Advertisments