Why is the format specifier for uint8_t and uint16_t the same (%u)? [duplicate]

烈酒焚心 提交于 2019-12-04 03:23:16

printf() is a variadic function. Its optional arguments( and only those ) get promoted according to default argument promotions( 6.5.2.2. p6 ).

Since you are asking for integers, integer promotions are applied in this case, and types you mention get promoted to int. ( and not unsigned int because C )

If you use "%u" in printf(), and pass it an uint16_t variable, then the function converts that to an int, then to an unsigned int( because you asked for it with %u ) and then prints it.

Because %u stands for "unsigned", it well may be uint64_t and is architecture dependent. According to man 3 printf, you may want to use length modifier to get sought behaviour, i.e. %hu (uint16_t) and %hhu (uint8_t).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!