C Intro - Two unsigned int subtract




#include <stdio.h>
#include <stdint.h>

int main()
{
   uint16_t t1 = 0, t2 = 0xFFFF, result;
   int16_t res;

   res = t1 - t2;

   // printf("0x%X\n", result);
   printf("%d\n", (int16_t)t2);  // output -1
   printf("0x%X\n", res);        // output 0x1
}


Compiler will convert them into int type, then do subtract,

then be parsed to be corresponding result.



int main()
 {
    uint16_t t1 = 3, t2 = 0xFF03, result;
    int16_t res;
   res = t1 - t2;
   // printf("0x%X\n", result);
    printf("%d\n", (int16_t)t2);  // output -253
    printf("0x%X\n", res);        // output 0x100
    printf("%d\n", res);        // output 256
 }



你可能感兴趣的:(c)