Printing Integer Numbers with PRI* in C

If the processor is 64 bits and we wanna use 32 integers to print/read them, we can use inttypes.h library to explicitly tells the processor which length we want to use.

#include <inttypes.h>

uint8_t 8bit_int;
int32_t 32bit_int;

printf("Hexadecimal format %" PRIx8 "\n");
printf("Integer format %" PRIi8 "\n");

printf("Hexadecimal format %" PRIx32 "\n");
printf("Integer format %" PRIi32 "\n");

First three letters:
PRI for printf format
SCN for scanf format (Not shown in the example, but it is used in a similar way witch scanf())

Fourth letter:
x for hexadecimal formatting
u for unsigned formatting
o for octal formatting
i for integer formatting
d for decimal formatting

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s