#include<stdio.h>
int
main()
{
char
a=250;
int
expr;
expr= a+ !a + ~a + ++a;
printf("%d",expr);
return 0;
return 0;
}
Output: -6
Explanation: char a =
250;
250 is beyond the range
of signed char. Its corresponding cyclic value is: -6 (-128 to 127), 0 to 127
is occupied in first 1 to 250, then it remains 122. Now this 122 is occupied by
-128 to -7 and remains -6.
So, a = -6
Consider
on the expression:
expr= a+ !a + ~a + ++a;
Operator! , ~ and ++
have equal precedence. And it associative is right to left.
So, First ++ operator
will perform the operation. So value a will -5
Now,
Expr = -5 + !-5 + ~-5 +
-5
= -5 + !-5 + 4 – 5
//(to solve ~-5 first
make binary of 5 then subtract 1 and convert back to decimal.)
= -5 + 0 + 4 -5
why did we subtract 1 while solving ~-5
ReplyDelete