Thursday, April 5, 2007

C Programming Tip on Unsigned Types

Do not use an unsigned type to represent a quantity just because it will be never negative (e.g. age or national_debt).


#include<stdio.h>
main() {
 int negative = -1;
 unsigned positive = 1;
 if(negative < positive)
  printf("This is what we want.\n");
 if(negative > positive)
  printf("It's just the semantics.\n");
}

Then guess what the output is?

No comments:

Post a Comment