When you're trying to understand something like this, it's often helpful to break the problem down into smaller pieces and look at each one. I modified your program so that it prints each of the values:
#include<stdio.h>
int main(){
char x = 250;
printf("x: %d\n", x);
printf("!x: %d\n", !x);
printf("~x: %d\n", ~x);
printf("++x: %d\n", ++x);
int ans = x + !x + ~x + ++x;
printf("ans: %d\n", ans);
}
And the output I get when I run it is:
x: -6
!x: 0
~x: 5
++x: -5
ans: -5
Once you understand what each part means, it's easier to see how they combine into the final result. However, my compiler also emits a warning because ++x
modifies x
in the same expression that uses x
in other places, and changing the order in which the terms are evaluated will change the final result.