I know this is silly question but I don’t know which step I’m missing to count so can’t understand why the output is that of this code.
int i=2;
int c;
c = 2 * - ++ i << 1;
cout<< c;
I have trouble to understanding this line in this code:
c = 2 * - ++ i <<1;
I’m getting result -12. But I’m unable to get it how is precedence of operator is working here?
Have a look at the C++ Operator Precedence table.
++iis being evaluated, yielding3.-is being evaluated, yielding-3.-6.-12.-12is being assigned to the variablec.If you used parentheses to see what operator precedence was doing, you’d get
Plus that expression is a bit misleading due to the weird spacing between operators. It would be better to write it
c = 2 * -++i << 1;1 Note that this is not the unary
*, which dereferences a pointer. This is the multiplication operator, which is a binary operator.