signed and unsigned integer

In C, 
int main() {
  unsigned int a = 10;
  int b = -19;
  puts((a+b)>0? "Positive":"Negative");
Explain the output. 


  1. The whole expression get typecasted to unsigned after evaluation? Is this the reason?

  2. @Spectatot
    Actually, the conversion rules are

    a) Convert to the larger type.
    b) In case of equal size prefer unsigned over signed.
    You can verify this by changing the declaration of int b to int64_t b (i.e long long int type). The output would now say Negative.
    NOTE: Don't forget to include stdint.h if you use int64_t..:)


Post a Comment

Popular posts from this blog