A correct program can look for example the following way
#include <iostream>
int main()
{
int num, a = 15;
num = ( ----a )--;
std::cout << num << std::endl;
std::cout << a << std::endl;
return 0;
}
Its output is
13
12
The post-decrement operator has a higher priority than the pre-decrement operator and its result is rvalue
that may not be changed.
This expression
----a--
is equivalent to
----( a-- )
and will not compile.
So you need to use parentheses to make the program to compile.
Take into account that a corresponding program written in C as for example
#include <stdio.h>
int main( void )
{
int num, a = 15;
num = ( ----a )--;
printf( "%d\n", num );
printf( "%d\n", a );
return 0;
}
will not compile because the pre-decrement operator in C also returns rvalue and you may not apply the operator to rvalue.
Only the C++ program is valid.
3
solved What is the output of this code? Am i missing something here?