what is the exact reason ?

Thread Tools Search this Thread
Top Forums Programming what is the exact reason ?
# 1  
Old 12-08-2001
Question what is the exact reason ?

please refer the following 2 statements...
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.. Smilie

# 2  
Old 12-08-2001
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;


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++;


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.


Last edited by parmenides; 12-08-2001 at 03:06 PM..
# 3  
Old 12-08-2001
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).
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. OS X (Apple)

What's the reason behind having -n option for mv command?

Sorry for a question that may seem dumb but learning UNIX basics I still can not grasp benefits of using mv -n source file target file I can understand the need for cp -n source file target file when you get a copy with contents untouched but the former baffles me. I know that this about... (8 Replies)
Discussion started by: scrutinizerix
8 Replies

2. AIX

Cluster failure reason

Hi guys ! I'm a French IT student in AIX, and i'm note very fluent in english. I have a task : Write a script to inform the administrator if on of a cluster UC is not working. I'm not going to ask you the script ^^' But i want to make a list of the failure reason of a cluster (network,... (10 Replies)
Discussion started by: Tharsan
10 Replies

3. Shell Programming and Scripting

echo exact xml tag from an exact file

Im stumped on this one. Id like to echo into a .txt file all names for an xml feed in a huge folder. Can that be done?? Id need to echo <name>This name</name> in client.xml files. $path="/mnt/windows/path" echo 'recording names' cd "$path" for names in $path than Im stuck on... (2 Replies)
Discussion started by: graphicsman
2 Replies

4. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

5. UNIX for Advanced & Expert Users

programs are being autostarted for no reason

I have nothing in these folders and several programs are being autostarted for no reason. Chromium, nautilus, Okular, and quadrapassel are the programs being started. I have nothing in my .bashrc. Not sure where else to look. I have no idea why this is happening and it is driving me crazy. ... (0 Replies)
Discussion started by: cokedude
0 Replies

6. Red Hat

Server usually restart not reason

Hi everyone, - I have CentOs server 5.4 , I usually remote by ssh. - My problem is server usually restart but I don't reason. I check log in file /var/log/messages: I don't see "signal 15" which kernel have to receive before restart. Everyone can see in attach. - I try to restart with command... (3 Replies)
Discussion started by: vietbk87
3 Replies

7. Programming

Reason for Segmentation fault

The following program fails with "Segmentation fault" error message, while I try to run in Ubuntu (Debian) Linux m/c. It is not creating any core file, so I could not cross examine it with the debugger. See the comments for much better understanding. Could any one tell me the exact reason why the... (20 Replies)
Discussion started by: royalibrahim
20 Replies
Login or Register to Ask a Question