Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > evaluate it

Reply
Thread Tools

evaluate it

 
 
ashim.seth@gmail.com
Guest
Posts: n/a
 
      07-05-2005
hey people i was just testin this:
int i=5,j;
j=i++ + ++i;
cout<<j;


and then...

int i=5;
int j= i++ + ++i;
cout<<j;


both givin different results
y?
plz help

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-05-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> hey people i was just testin this:
> int i=5,j;
> j=i++ + ++i;
> cout<<j;
>
>
> and then...
>
> int i=5;
> int j= i++ + ++i;
> cout<<j;
>
>
> both givin different results
> y?
> plz help


Read the FAQ, section 39

V
 
Reply With Quote
 
 
 
 
Peter Julian
Guest
Posts: n/a
 
      07-05-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> hey people i was just testin this:
> int i=5,j;
> j=i++ + ++i;
> cout<<j;
>
>
> and then...
>
> int i=5;
> int j= i++ + ++i;
> cout<<j;
>
>
> both givin different results
> y?
> plz help
>


The results are irrelevent. Since you are refering to the same variable in
that compound statement, the standard does not guarentee when, how or
whether the variable i gets incremented. In fact, the standard has a
requirement that the said variable be only modified once in between sequence
points.

Try:

#include <iostream>

int main()
{
int i = 5;

std::cout << "i = " << i << "\n";

int j(i++);

std::cout << "i = " << i << "\n";
std::cout << "j = " << j << "\n";

j += (++i);

std::cout << "i = " << i << "\n";
std::cout << "j = " << j << "\n";

return 0;
}

/*
i = 5
i = 6
j = 5
i = 7
j = 12
*/

 
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
70-547 Evaluate Proof of Concept =?Utf-8?B?THVsdQ==?= MCSD 1 01-31-2006 03:24 AM
Evaluate Microsoft Skills Assessment Support Information (SASI) si =?Utf-8?B?RGlzbmV0IEFkbWluaXN0cmF0b3I=?= MCSE 1 02-24-2005 04:08 PM
optimize error:left bound range doesn't evaluate to a const. senthil VHDL 0 03-04-2004 03:02 AM
How to evaluate control Yuriy ASP .Net 0 08-12-2003 03:19 PM
Function to evaluate whether a string is all UPPER Brad Smith ASP .Net 2 06-27-2003 04:14 PM



Advertisments