Hacker News new | past | comments | ask | show | jobs | submit login

I too, love coding in C. I love how you can control almost everything. There's nothing "mysterious" or abstracted away. Maybe I'm OCD, but I love understanding exactly what is going on in my programs at all times



  #include <stdio.h>
  int main() {
      char a = 3;
      char b = 8;
      char result = a * b;
      printf("%u %u %u %u", sizeof(a), sizeof(b), sizeof(result), sizeof(a * b));
      return 0;
  }
PRINTOUT/x86-64: 1 1 1 4

Surprising results on x86, probably not on Arduino or embedded platforms.

The phenomenon is integer promotion in C.

EDIT: "Char isn't an arithmetic type!" The same is true for short, too.

EDIT2: The consequences are: Might need to manually %= 0x100 instead of relying on data type's inherent modulation in multi-operational arithmetic, making rotation a necessarily pre-scan operation for LE/BE conversion instead of a process-transparent operation and I had a third one... hm. Also, maybe speed loss?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: