Site Tools


wiki:h-inttypes

Table of Contents

<inttypes.h>

<inttypes.h> solves a portability trap: uint64_t may be unsigned long on one platform and unsigned long long on another. The format macros (PRIu64, PRIx64) expand to the correct printf/scanf specifier for each platform.

Use PRIu64, PRId64, etc. instead of guessing %llu or %lu.

Example

This example prints fixed-width integers portably using the PRI macros.

// compile: gcc -o inttypesexample inttypesexample.c
// run: ./inttypesexample
// description: portable printf of fixed-width integers
 
#include <inttypes.h>
#include <stdio.h>
 
int main() {
    uint8_t a = 255;
    uint32_t b = 0xDEADBEEF;
    uint64_t c = UINT64_MAX;
    int64_t d = INT64_MIN;
 
    printf("uint8:  %" PRIu8 "\n", a);
    printf("uint32: %" PRIu32 " (0x%" PRIx32 ")\n", b, b);
    printf("uint64: %" PRIu64 "\n", c);
    printf("int64:  %" PRId64 "\n", d);
 
    return 0;
}
wiki/h-inttypes.md · Last modified: by 127.0.0.1