Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is unquoted `PRIu32` in printf in C?

Tags:

c

printf

uint32

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.

like image 315
Alex Avatar asked Aug 28 '17 15:08

Alex


1 Answers

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.

like image 57
Some programmer dude Avatar answered Nov 06 '22 13:11

Some programmer dude