I need to use printf() to print a uint16_t. This SO answer (How to print uint32_t and uint16_t variables value?) says I need to use inttypes.h.
However, I'm working on an embedded system and inttypes.h is not available. How do I print a uint16_t when the format specifier for a uint16_t is not available?
short int
is the smallest at least 16 bits long so convert the value tounsigned short int
and print it with%hu
.You should use the style of inttypes.h but define the symbols yourself. For example:
Figure them out for your machine and use them. Take a look at others in inttypes.h and figure which you will need.
This way, your code will be more portable. I've been doing embedded systems work since the late 70's. Trust me: portability is important.
An obvious way is:
The unsigned int is guaranteed to be at least 16 bits, so this is not a lossy conversion.