Convert int to uint8_t

this is the correct way to convert int value to uint8_t :

 int x = 3; uint8_t y = (uint8_t) x; 

suppose x will never be less than 0. Although gcc does not give any warnings for the above lines, I just wanted to be sure whether to do it right or is there a better way to convert int to uint8_t?

PS I use C on Linux if you are going to offer a standard feature

+4
source share
1 answer

This is correct, but casting is not required:

 uint8_t y = (uint8_t) x; 

equivalently

 uint8_t y = x; 

x implicitly converted to uint8_t before initialization in the declaration above.

+9
source

Source: https://habr.com/ru/post/1493670/


All Articles