C
Programming and Technical
Programming
Output
Find the Output.
int main(){
char a=250;
int expr;
expr= a+ !a + ~a + ++a;
printf("%d",expr);
return 0;
}
Options:249,250,0,-6
Read Solution (Total 2)
-
- 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
= -6
- 8 years agoHelpfull: Yes(2) No(0)
- Ans : -6
1) evaluate ++a and assign it a. a=251.. but since its signed char the corresponding integer value is -5 (it exceeds 128)
2) ~a=~251=4
3)!a=0
4)expr = -5+0+4+-5=-6 - 8 years agoHelpfull: Yes(1) No(3)
C Other Question