Which integral promotions do take place when printing a char?

后端 未结 3 1398
深忆病人
深忆病人 2021-01-11 15:15

I recently read that

unsigned char x=1;
printf(\"%u\",x);

invokes undefined behaviour since due to the format specifier %u, printf expects

3条回答
  •  粉色の甜心
    2021-01-11 15:56

    If your platform's int can represent all values than an unsigned char can, then the promotion is to int, otherwise to unsigned int. So it depends on your platform.

    As to "why", that's because you're passing x as a variable argument, and the rules of variable arguments say that the standard promotions take place (presumably so as to simplify the implementation).

提交回复
热议问题