Format specifiers for uint8_t, uint16_t, ...?

Viewed 77356

If I have an integer variable I can use sscanf as shown below by using the format specifier %d.

sscanf (line, "Value of integer: %d\n", &my_integer);

Where can I find format specifiers for uint8_t, uint16_t, uint32_t and uint64_t?

uint64_t has probably %lu.

7 Answers

You can check types in <cstdint> for C++ or in <types.h> for C. Then you can specify format which is currently compiled on your machine. If variable is unsigned then you need to use %u, if variable is signed, then use %d. For variables size lower than 32 bytes you don't have to specify long prefix.

In my configuration I have 64 bit program, so my uint64_t is compiled as unsigned long long int, so it means to use %llu. If your program will be compiled in 32 bit you can use %lu because uint64_t will be compiled as unsigned long int

But who is using x86 still? :), so generally:

uint8_t, uint16_t, uint32_t - %u
uint64_t - %llu
int8_t, int16_t, int32_t - %d
int64_t -%lld
Related