Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Unpredictab...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 14 Topic 45817 of 46738
Post > Topic >>

Re: Unpredictable result of Increment operation

by Jack Klein <jackklein@[EMAIL PROTECTED] > May 8, 2008 at 09:46 PM

On Thu, 8 May 2008 17:12:15 -0700 (PDT), bintom
<binoythomas1108@[EMAIL PROTECTED]
> wrote in comp.lang.c++:

> Is there any reason why 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

For build-in types, such as int, the behavior of the first two is
undefined.  You are attempting to modify the value of 'i' more than
once without a sequence point in between.  The output could be
anything, as far as the language is concerned.

The last two are errors because the post increment operator on a
built-in type does not yield an object, instead it yields the value
that the object had before the post increment.  So it is the same as
if you had written:

  cout << 1++;
  cout << ++1;

You can't apply the increment operators to a value.

I'd suggest you consult your C++ book about sequence points and
undefined behavior.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.para****ft.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 




 14 Posts in Topic:
Unpredictable result of Increment operation
bintom <binoythomas110  2008-05-08 17:12:15 
Re: Unpredictable result of Increment operation
Ian Collins <ian-news@  2008-05-09 12:16:10 
Re: Unpredictable result of Increment operation
"Victor Bazarov"  2008-05-08 20:54:12 
Re: Unpredictable result of Increment operation
Ian Collins <ian-news@  2008-05-09 13:31:15 
Re: Unpredictable result of Increment operation
Ian Collins <ian-news@  2008-05-10 20:55:51 
Re: Unpredictable result of Increment operation
Jack Klein <jackklein@  2008-05-08 21:46:21 
Re: Unpredictable result of Increment operation
bintom <binoythomas110  2008-05-09 01:02:00 
Re: Unpredictable result of Increment operation
James Kanze <james.kan  2008-05-09 06:10:12 
Re: Unpredictable result of Increment operation
"Victor Bazarov"  2008-05-09 10:18:03 
Re: Unpredictable result of Increment operation
brno <brno.barutchek@[  2008-05-10 10:34:32 
Re: Unpredictable result of Increment operation
Ian Collins <ian-news@  2008-05-10 21:01:24 
Re: Unpredictable result of Increment operation
"Victor Bazarov"  2008-05-10 09:47:14 
Re: Unpredictable result of Increment operation
James Kanze <james.kan  2008-05-10 08:30:56 
Re: Unpredictable result of Increment operation
James Kanze <james.kan  2008-05-10 08:37:20 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Jul 8 23:56:32 CDT 2008.