473,508 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6706
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
1835
by: Chris Yates | last post by:
Why is this illegal: --(i++) // Error: -- needs an l-value and this is not: (--i)++ ??
98
14307
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
14
42344
by: deko | last post by:
Is there a way to check user input for illegal characters? For example, a user enters something into a text box and clicks OK. At that point I'd like to run code such as this: illegal =...
6
7252
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my tests confirm this. However, most of the Math...
12
2357
by: [Yosi] | last post by:
What I should do to return back the permissin I had in VS2003 , that allowd me to access public methode of one Class from other Thread. I have Class A(FORM) which create new thread (new class B),...
6
13527
by: BlueTrin | last post by:
Hello I was adapting a C version of SolvOpt in C++ to use it within a virtual class. However I am stuck with the overriding of evaluation and gradiant functions. cStepCurveEvaluator.cpp...
13
6153
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
2
6603
by: dobbouk | last post by:
I'm getting these 2 errors and haven't got a clue whats causing them please java 43: illegal start of type for (i=0;i< Number_of_squares; i++) java 215 identifier expected } ...
13
9803
by: jehugaleahsa | last post by:
Hello: In C++, you had to distinguish between post and pre increments when overloading. Could someone give me a short demonstration of how to write these? I get the impression that are...
8
2068
by: bintom | last post by:
Why doe the following C++ code behaves as it does? int i; i=1; cout << (++i)++; Output: 2 i=1; cout << ++(++i); Output: 3 i=1; cout << (i++)++; Output: Error. LValue required...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7495
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.