int to char casting

前端 未结 4 1611
鱼传尺愫
鱼传尺愫 2020-12-16 18:41
int i = 259;       /* 03010000 in Little Endian ; 00000103 in Big Endian */
char c = (char)i;  /* returns 03 in both Little and Big Endian?? */

In

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 19:18

    Endianness doesn't actually change anything here. It doesn't try to store one of the bytes (MSB, LSB etc).

    • If char is unsigned it will wrap around. Assuming 8-bit char 259 % 256 = 3
    • If char is signed the result is implementation defined. Thank you pmg: 6.3.1.3/3 in the C99 Standard

提交回复
热议问题