title: What is the rule for C to cast between short and int?
body: <p>I'm confused when using C to cast between short and int. I assume short is 16-bit and int is 32-bit. I tested with below code:</p>
  
  <pre><code>unsigned short a = 0xFFFF;
  signed short b = 0xFFFF;
  
  unsigned int u16tou32 = a;
  unsigned int s16tou32 = b;
  signed int u16tos32 = a;
  signed int s16tos32 = b;
  
  printf("%u %u %d %d\n", u16tou32, s16tou32, u16tou32, s16tou32);
  </code></pre>
  
  <p>What I got is:</p>
  
  <ul>
  <li>u16tou32: 65535 </li>
  <li>s16tou32: 4294967295 </li>
  <li>u16tos32: 65535 </li>
  <li>s16tos32: -1</li>
  </ul>
  
  <p>What I am confused with is the conversion between s16 to u32, as well as u16 to s32. Seems like s16 to u32 is doing a "sign extension", while u16 to s32 is not. What exactly is the rule behind this? Also is this implementation-dependent? Is it safe to do this type of casting in C, or should I use bit manipulation myself to avoid unexpected results?</p>