How do I printf() a uint16_t?
Asked Answered
C

3

31

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?

Chappelka answered 18/3, 2015 at 1:53 Comment(3)
printf("%u\n", (uint16_t)something);Feces
Figure out how wide the various integer types are on your system, and use the appropriate specifier.Sarcocarp
@user3528438: No, %u expects an unsigned int, not a uint16_t. The cast should be to unsigned int.Decomposed
O
53

You should use the style of inttypes.h but define the symbols yourself. For example:

#define PRIu8 "hhu"
#define PRId8 "hhd"
#define PRIx8 "hhx"
#define PRIu16 "hu"
#define PRId16 "hd"
#define PRIx16 "hx"
#define PRIu32 "u"
#define PRId32 "d"
#define PRIx32 "x"
#define PRIu64 "llu" // or possibly "lu"
#define PRId64 "lld" // or possibly "ld"
#define PRIx64 "llx" // or possibly "lx"

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.

Ofay answered 15/6, 2018 at 21:16 Comment(1)
I could not come up with a better answer! Portability is often underestimated indeed.Quadrangular
U
22

An obvious way is:

printf("%u\n", (unsigned int)x);

The unsigned int is guaranteed to be at least 16 bits, so this is not a lossy conversion.

Unbending answered 18/3, 2015 at 1:55 Comment(4)
Indeed anything else would be over-engineering.Drat
If you're certain the type would never change, this is fine. A big advantage to inttypes.h is that if you change the data type but forget to change the format string, the compiler will warn you. With the above, if x is changed to a uint64_t, the high order bits would not be printed. Admittedly, such a big change isn't common, but it happens, for example when the value is a key that changes from a serial number to a UUID. When taking shortcuts like this, context is important.Ofay
@adabsurdum the default argument promotions would take x to int, whereas the %u specifier requires unsigned int argumentUnbending
@adabsurdum the rules in the standard apply to the program, not the internals of the implementation ; the standard only requires that PRIu16 works as described; doesn't mandate anything about intermediate steps between that.Unbending
D
9

short int is the smallest at least 16 bits long so convert the value to unsigned short int and print it with %hu.

Danadanae answered 18/3, 2015 at 1:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.