Format specifiers for integers of a fixed width?

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

Using% d,% i, etc. will not lead to program porting. Are PRIxx macros a better approach?

+3
source share
2 answers

Are PRIxx macros a better approach?

As far as I know, yes.

Edit: another solution is to apply to the type that is at least the same as the one you want to print. For example, inthas a width of at least 2 bytes to print int16_twith printf("%d\n", (int)some_var).

+3
source

, , .

, , .

, :

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

int main (void) {
    int32_t i32 = 40000;
    printf ("%d\n", i32);            // might work.
    printf ("%" PRId32 "\n", i32);   // will work.
    return 0;
}

.

, , . , 16- int .

0

Source: https://habr.com/ru/post/1706923/


All Articles