Consider the following code:
#include <stdio.h>
#include <inttypes.h>
void main(void)
{
int32_t a = 44;
fprintf(stdout, "%d\n", a);
fprintf(stdout, "%"PRId32"\n", a);
}
When is it better to use %d and when would it be better to use "%"PRId32? How do both of those format characters differ? Does this have something to do with how int32_t is typedeffed on your hardware/machine?
Use %d for int and PRId32 for int32_t.
Maybe on your platform int32_t is just a typedef for int but this is not true in general.
Using a wrong specifier invokes undefined behavior and should be avoided.
As pointed out by @EricPostpischil note that %d is used inside a quoted format string while PRId32 is used outside. The reason for this is that PRId32 is a macro which expands to a string literal and is then concatenated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With