Resources

People often ask me "How did you learn how to hack?" The answer: by reading. This page is a collection of the blog posts and other articles that I have accumulated over the years of my journey. Enjoy!

If you know why x > y, you're a good C developer!- 1585

Dave W PlummerPosted 1 Year Ago
  • The post is a simple C screenshot:
    int main(){
      int x = -10; 
      unsigned int y = 5; 
      if(x > y)
        printf("x is greater than y"\n);
      else
        printf("y is less than x\n";
      }
    
  • There are many different types of numbers in C. Integers differ in length (1, 2, 4, 8 bytes) and signed/unsigned. Additionally, there are floats that can store more precise numbers and larger numbers than floats but have gaps in them. I assumed that this code wouldn't compile without a cast. A friend of mine assumed that the right side would be casted to the right side, which was always wrong.
  • To move between these types with casting would be super annoying to do. So, C has some ranking system for automatic type casting. The "rank" of the unsigned integer takes precedence here. So, x is changed to an unsigned integer. Regarldess if this uses the - as a part of the number or not, it becomes bigger than 5.
  • "Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type."
  • C is fun!