int main()
{
int a=10;
if (a == a--)
printf("true1\t\n");
a=10;
if(a == --a)
{
printf("true2");
}
return 0;}
as in the second if condition a=10 and --a is 9 i.e 10 != 9 so how come the second condition is evaluated ?
int main()
{
int a=10;
if (a == a--)
printf("true1\t\n");
a=10;
if(a == --a)
{
printf("true2");
}
return 0;}
as in the second if condition a=10 and --a is 9 i.e 10 != 9 so how come the second condition is evaluated ?
The value of --a
is the previous value of a
minus 1
.
Furthermore that expression has the side-effect of changing the value of a
.
The left part of the comparison is the value of a
... but is it the value of a
before or after the side-effect has been applied?
The C Standard does not force the sequence of checking the value and applying the side-effect; and says that reading the value of a variable and changing its value without an intervening sequence point is Undefined Behaviour.
Basically there is a sequence point at every ;
in the program (it's not as straightforward); your expression (a == --a
) does not have sequence points.
I believe the next rule is also valid in C as in Java : The --x has a higher priority than x--
a=10;
printf(a--) // should be showing 10;
a=10;
printf(--a) //should be showing 9