Increment and Decrement Operators
-
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