473,666 Members | 2,073 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 6715
In article <eo*********@nh .pace.co.uk>,
news.pace.co.uk <st********@pac e.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.u k" <st********@pac e.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[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Jan 16 '07 #3
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <eo*********@nh .pace.co.uk>,
news.pace.co.uk <st********@pac e.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.u k" <st********@pac e.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_Keit h) 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.u k" 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********@pac e.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
1850
by: Chris Yates | last post by:
Why is this illegal: --(i++) // Error: -- needs an l-value and this is not: (--i)++ ??
98
14403
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
42399
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 = Array(\, /, :, *, ?, ", <, >, |) If Me.TextBox Contains illegal Then MsgBox "You entered illegal characters. Please try again." Me.TextBox = Null
6
7264
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 methods deal with doubles and I'm having problems with casting them back to floats. For example: (double)0.1f = 0.10000000149011612 I don't need the accuracy of doubles, but the speed of floats would be benificial, but it seems I'm either...
12
2371
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), the new class thread get as parameter reference to his father( CLASS who mad it (classA)), The new thread need to call methodes from his father like myfathe.methode(), This worked in VS 2003 but now in VS 2005 Beta throw an exception : An...
6
13566
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 cStepCurveEvaluator.cpp(14) : error C2296: '.*' : illegal, left operand
13
6181
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 ------------------ b += (float)(mode *val); On 32 bit(intel , vs 2003, C++), some watch variables are
2
6623
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 } heres the whole code
13
9826
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 handled with the same overload, but I just want to make sure.
8
2082
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 i=1; cout << ++(i++); Output: Error. LValue required
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8783
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8552
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5666
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2773
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 we have to send another system
2
1776
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.