The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
why my script stopped- any reason(urgent please) krishna9 Shell Programming and Scripting 1 05-21-2008 08:55 AM
Reason for Segmentation fault royalibrahim High Level Programming 20 12-13-2007 06:26 PM
Reason for EXPORT in .profile,env,etc sumeet Shell Programming and Scripting 1 03-01-2007 02:12 PM
Thread was closed without a valid reason patras Post Here to Contact Site Administrators and Moderators 2 08-23-2006 09:45 PM
File not found - for no reason moomintroll Filesystems, Disks and Memory 4 08-24-2005 09:35 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 12-08-2001
Registered User
 

Join Date: Sep 2001
Location: El Net-3L Academy,NO. 69, Rd. 19(Old), 9A(New), Dhanmondi,Dhaka, Bangladesh-1209.
Posts: 6
Question what is the exact reason ?

please refer the following 2 statements...
1)
int i=1,j;
j= i++ + i++;

2) int i=1,j;
j=++i + ++i;

how j becomes 2 and 6 for the above 2 statements
respectively ???
( i guessed j must be 3 and 5)
Somebody define the exact reason please..

-sham-
__________________
u help me, He'll help u.
Reply With Quote
Forum Sponsor
  #2  
Old 12-08-2001
Registered User
 

Join Date: Nov 2001
Posts: 2
The effect of the '++' (and all of this applies to '--' as well) operator changes depending on where you put it. If your put it before the variable you're modifying, then the program will increment the variable before using the variable in any sort of statement, thus

a = ++b;

means

b = b + 1;
a = b;

However, if the operator comes after the variable, then the increment is executed after any expressions the variable is in.

b = a++;

means

b = a;
a = a + 1;


The first statement performs all the adding before all the incrementing, so i remains at 1 until after the value is assigned to j.

j = i + i; // j is 2
i = i + 1; // i is now 2
i = i + 1; // i is now 3

The second statement does all the incrementing first, so it adds one to i twice before adding it two itself, and assigning that value to j.

i = i + 1; // i is now 2
i = i + 1; // i is now 3
j = i + i; // j is now 6 (3+3)

If you switched either of the incrementors in either statement to the other side, then you (ought) to get 4.

P.

Last edited by parmenides; 12-08-2001 at 12:06 PM.
Reply With Quote
  #3  
Old 12-08-2001
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
Well it should be obvious that your compiler is doing all pre-increments before the addition and all post-increments after the addition.

So 2 and 6 are legal values for your code. So would be 3 and 5. Any behavior at all is legal according to the standards. This is because your code is illegal. The standards do not prescribe what the compiler must do with illegal code. In an ideal world, your code would not compile. Once you attach a pre or post increment to a variable it is illegal to reference that variable again until the next "sequence point" (basicly a comma or semicolon).
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0