What is unquoted `PRIu32` in printf in C?

Alex picture Alex · Aug 28, 2017 · Viewed 11.3k times · Source

I am looking at the following code:

#include <stdio.h>
#include <inttypes.h>

int main()
{
    uint32_t total = 0;

    printf("\tTotal: %"PRIu32"\n\n", total);
    return total;
}

How does PRIu32 fit into the printf syntax? I mean, I sorta can guess that, Iu32 probably means "Integer unsigned 32-bit". However, I am not sure which form found in man 3 printf would accommodate variables outside quotation marks-- and how this can generalize to other statements outside quotation marks.

Answer

Some programmer dude picture Some programmer dude · Aug 28, 2017

It's a format macro constant.

They are used for portable formatting of values along different platforms where sizes of the primitive number types might differ.

The one in the question is the format to print unsigned 32-bit integers in decimal format.

These macros works because C concatenates consecutive constant string literals. For example the three strings "\tTotal: %" "u" "\n\n" will be concatenated into the single string "\tTotal: %u\n\n" by the compiler.