I have an unsigned int variable x which value is comprised between from 0 and 0xFFFFF and I would like to print it in a way more readable for the user.
If x is greater than 0xF000, print it in hexadecimal, else print it in decimal.
So something like this would do it
if (x > 0xF000)
printf("%#x", x);
else
printf("%u", x)
But I would like to know if there is an handier and smarter way to handle this, like conditional formatting for output depending of their value.
0x
, which you can do either with"0x%x"
or"%#x"
.printf
with a format which is not a single, constant string is arguably poor style. It is disrecommended by some style guides. I believe it is diagnosed by some compilers. It means that the compiler can't, in general, do any checking for mismatches between the format string and the arguments.