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 03:06 PM..
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).
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)
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)
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)
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)
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)
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)
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)