Postfix form of ++ operator - follows the use-then-change rule,
The prefix form (++ x, - x) follows the change-then-use rule.
Example 1:
When several values are cascaded with <using cout, then the calculations (if any) are performed from right to left, but printing is done from left to right, for example, (if val, if initially 10)
cout<< ++val<<" "<< val++<<" "<< val;
will result in
12 10 10
Example 2:
In Turbo C ++, if several occurrences of ++ or (in any form) are found in the expression, then all prefix forms are calculated first, then the expression is calculated and, finally, the final forms are written, for example,
int a=10,b; b=a++ + ++a + ++a + a; cout<<b<<a<<endl;
It will be output in Turbo C ++ will
48 13
While it is output in a modern compiler, there will be (because they strictly follow the rules)
45 13
- Note. Multiple use of increment / decrement operators on one variable in one expression is not recommended. The processing / results of such expressions vary from compiler to compiler.
Sunil Dhillon Nov 07 '15 at 7:27 2015-11-07 07:27
source share