![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#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. |
|
#3
|
||||
|
||||
|
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). |
||||
| Google The UNIX and Linux Forums |