Data Type Truncation

后端 未结 2 1595
小蘑菇
小蘑菇 2021-01-27 11:42

I have a basic C question.

Suppose I\'ve declared and initialized a standard 16 bit unsigned integer

uint16_t var1 = 0x1234;

and then s

相关标签:
2条回答
  • 2021-01-27 12:10

    Yes. The compiler would internally interpret this as

    var2 = (uint8_t)var1;
    

    which would result in var2 having the value 0x34.

    0 讨论(0)
  • 2021-01-27 12:36

    I think you mean:

    uint16_t var1 = 0x1234;
    uint8_t var2;
    var2 = var1;
    

    Yes, this will truncate var1 to fit within var1's data type, so var2 will be equal to 0x34.

    0 讨论(0)
提交回复
热议问题