Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format specifiers for fixed-width integers?

Tags:

c

What are the format specifiers to use for printf when dealing with types such as int32_t, uint16_t and int8_t, etc.?

Using %d, %i, etc. will not result in a portable program. Is using the PRIxx macros the best approach?

like image 236
user94592 Avatar asked Apr 23 '26 16:04

user94592


1 Answers

Is using the PRIxx macros the best approach?

As far as I know, yes.

Edit: another solution is to cast to a type that is at least as wide as the one you want to print. For example int is at least 2 bytes wide, to can print a int16_t with printf("%d\n", (int)some_var).

like image 120
Bastien Léonard Avatar answered Apr 25 '26 07:04

Bastien Léonard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!