Increment and Decrement Operators
-
no no..in any compiler prefix operator execute first than postfix. postfix will execute after value returned or assigned to some variable..
-
O God....wow I am not the only one who is confused? This is the problem I specified when I started this thread, I got surprising answers when i solve these inc and dec problems.So who is correct please tell me and whom explaination I go with to pursue my learning on this topic?
Pre- and post- fix increment and decrement operations are pretty easy in theory, it's only when people get creative that you get problems in practice. Basically, a prefix (++i or --i) says to increase or decrease the value before you use the variable, so the variable has the new value immediately:
i = 10;
x = ++i + 5;Can be read as:
i = 10;
i = i + 1;
x = i + 5;and similarly for the -- version:
i = 10;
x = --i + 5;Can be read as:
i = 10;
i = i - 1;
x = i + 5;The postfix version (i++ or i--) does the same thing, but after the variable has been used:
i = 10;
x = i++ + 5;Can be read as:
i = 10;
x = i + 5;
i = i + 1;And similarly
i = 10;
x = i-- + 5;Can be read as:
i = 10;
x = i + 5;
i = i - 1;The trouble comes when you start mixing operations on the same line (as Richard said):
i = 10;
x = ++i + i++;The problem is that the language specification does not define exactly when pre- and post- fix operations should occur! Which means that it's implementation specific exactly what you get as a result: The value of
i
should always be the same: 12 but the value ofx
can be different depending on the compiler (and to an extent on the target processor - ARM for example has built in pre- and post- fix increment and decrement to it's "machine code" LOAD operations, so it would be quite likely that an efficient compiler would use them directly) Should it be executed as:i = 10;
i = i + 1;
x = i + i;
i = i + 1;Which gives the result 22 or as
i = 10;
i1 = i;
i = i + 1;
x = i1 + i;
i = i + 1;Which gives 21 Or as
i = 10;
i1 = i;
i = i + 1;
x = i + i1;
i = i + 1;which also gives 21 by a different route And bear in mind that the compiler does not have to evaluate the two operands of "+" in left to right order, so it could even give some very strange and unexpected results! Like 23... So avoid combining them: use them for "simple expressions" such as incrementing an array index each time round a loop, but don't get fancy, or your code may well fail in interesting ways... :laugh:
Never underestimate the power of stupid things in large numbers --- Serious Sam
-
hey i want to rectify Message send by Richard MacCutchan
int i = 10;
int result;
result = i++; // result equals 10, i equals 11
result = ++i; // result equals 12, i equals 12
result = i--; // result equals 12, i equals 11
result = --i; // result equals 10, i equals 10// note do not use expressions such as
result = ++i + i++; // results undefinedAs written the last line gives output undefine i think its wrong .it will give correct output as 23.
If u had checked then u already got the result.
here i will explan u how it's work..1:from left to right ++i changes i values 10 to 11.
2:then it will add 11 + 11 (as i++ is post increment operator it will execute after expression execution) and gives result as 22
3: then i values becomes 22 and i++ will going to execute and i value changes to 22+1=23You are making unwarranted assumptions! :laugh: It's more complex that that - Richard is right, you shouldn't mix 'em - see my post below for some compiler nasties...
Never underestimate the power of stupid things in large numbers --- Serious Sam
-
Pre- and post- fix increment and decrement operations are pretty easy in theory, it's only when people get creative that you get problems in practice. Basically, a prefix (++i or --i) says to increase or decrease the value before you use the variable, so the variable has the new value immediately:
i = 10;
x = ++i + 5;Can be read as:
i = 10;
i = i + 1;
x = i + 5;and similarly for the -- version:
i = 10;
x = --i + 5;Can be read as:
i = 10;
i = i - 1;
x = i + 5;The postfix version (i++ or i--) does the same thing, but after the variable has been used:
i = 10;
x = i++ + 5;Can be read as:
i = 10;
x = i + 5;
i = i + 1;And similarly
i = 10;
x = i-- + 5;Can be read as:
i = 10;
x = i + 5;
i = i - 1;The trouble comes when you start mixing operations on the same line (as Richard said):
i = 10;
x = ++i + i++;The problem is that the language specification does not define exactly when pre- and post- fix operations should occur! Which means that it's implementation specific exactly what you get as a result: The value of
i
should always be the same: 12 but the value ofx
can be different depending on the compiler (and to an extent on the target processor - ARM for example has built in pre- and post- fix increment and decrement to it's "machine code" LOAD operations, so it would be quite likely that an efficient compiler would use them directly) Should it be executed as:i = 10;
i = i + 1;
x = i + i;
i = i + 1;Which gives the result 22 or as
i = 10;
i1 = i;
i = i + 1;
x = i1 + i;
i = i + 1;Which gives 21 Or as
i = 10;
i1 = i;
i = i + 1;
x = i + i1;
i = i + 1;which also gives 21 by a different route And bear in mind that the compiler does not have to evaluate the two operands of "+" in left to right order, so it could even give some very strange and unexpected results! Like 23... So avoid combining them: use them for "simple expressions" such as incrementing an array index each time round a loop, but don't get fancy, or your code may well fail in interesting ways... :laugh:
Never underestimate the power of stupid things in large numbers --- Serious Sam
-
Thanks sir for highlighting its deep concepts related to the compiler. I hope now I am able to solve these question without getting shocked by their answers. Appreciate all your responses.
You're welcome!
Never underestimate the power of stupid things in large numbers --- Serious Sam
-
You are making unwarranted assumptions! :laugh: It's more complex that that - Richard is right, you shouldn't mix 'em - see my post below for some compiler nasties...
Never underestimate the power of stupid things in large numbers --- Serious Sam
-
I know increment and decrement operator depend on compiler. actually i had tried it in dev c++.
If you know that something is compiler dependant, then you can't say "it is like this" - because there is a very good chance that the other person is not using the same compiler! :laugh:
Never underestimate the power of stupid things in large numbers --- Serious Sam
-
These operators merely add (
inc
) or subtract (dec
) 1 from the item in question. If the operator is used in the postfix position (i++
) then the original value is returned. If the operator is used in the prefix position (--i
) then the new value is returned. Thus:int i = 10;
int result;
result = i++; // result equals 10, i equals 11
result = ++i; // result equals 12, i equals 12
result = i--; // result equals 12, i equals 11
result = --i; // result equals 10, i equals 10// note do not use expressions such as
result = ++i + i++; // results undefinedVeni, vidi, abiit domum
Am I the only one to spot that at least one of the problems in your last line is not related to increment and decrement at all? The
operator+
which is invoked here needs to evaluate both of its operands, but it's undefined whether the first or second operand is evaluated first - the compiler may choose to do either! So, if the compiler evaluates the first operand first, the following happens:i = 10;
i = i + 1; // pre-increment of the first argument
first_arg = i;
second_arg = i;
i = i + 1; // post-increment of the second argument
result = first_arg + second_arg; // 11 + 11Now consider the same on the assumption the compiler evaluates the second operand first:
i = 10;
second_arg = i;
i = i + 1; // post-increment of second argument
i = i + 1; // pre-increment of first argument
first_arg = 1;
result = first_arg + second_arg; // 12 + 10In this case, the results are the same, but that is just by coincidence - if you had used subtraction in stead of addition you'd be in for a surprise! You may want to check http://en.cppreference.com/w/cpp/language/eval_order[^] for further information regarding both function argument evaluation and increment/decrement.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
Am I the only one to spot that at least one of the problems in your last line is not related to increment and decrement at all? The
operator+
which is invoked here needs to evaluate both of its operands, but it's undefined whether the first or second operand is evaluated first - the compiler may choose to do either! So, if the compiler evaluates the first operand first, the following happens:i = 10;
i = i + 1; // pre-increment of the first argument
first_arg = i;
second_arg = i;
i = i + 1; // post-increment of the second argument
result = first_arg + second_arg; // 11 + 11Now consider the same on the assumption the compiler evaluates the second operand first:
i = 10;
second_arg = i;
i = i + 1; // post-increment of second argument
i = i + 1; // pre-increment of first argument
first_arg = 1;
result = first_arg + second_arg; // 12 + 10In this case, the results are the same, but that is just by coincidence - if you had used subtraction in stead of addition you'd be in for a surprise! You may want to check http://en.cppreference.com/w/cpp/language/eval_order[^] for further information regarding both function argument evaluation and increment/decrement.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
If you know that something is compiler dependant, then you can't say "it is like this" - because there is a very good chance that the other person is not using the same compiler! :laugh:
Never underestimate the power of stupid things in large numbers --- Serious Sam