Skip to content
Tutorials Center
My learning

Topics

C Reference ยท ctype.h, math.h and others

C inttypes.h: PRId32, SCNu64 and strtoimax

Reference for inttypes.h: print and read fixed-width integers portably with PRId32, PRIx64 and SCNu16, plus strtoimax, strtoumax, imaxabs and imaxdiv.

<inttypes.h> solves a small but annoying problem: which printf conversion matches int64_t? On Linux the type is long and needs %ld, on Windows it is long long and needs %lld. The header provides macros such as PRId64 that expand to the correct length modifier and conversion letter for the current platform, so the same source works everywhere.

It includes <stdint.h>, so you get all the fixed-width types as well. It also declares a few functions for intmax_t, the widest integer type: strtoimax, strtoumax, imaxabs and imaxdiv.

C
#include <inttypes.h>

int64_t total = 5000000000;
printf("total = %" PRId64 "\n", total);

Try it

C Printing and reading fixed-width integers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C strtoimax, imaxdiv and imaxabs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
#include <inttypes.h>

PRId8  PRId16  PRId32  PRId64        /* decimal, signed */
PRIi...                               /* signed, for %i */
PRIu... PRIo... PRIx... PRIX...      /* unsigned decimal, octal, hex (lower, upper) */
PRIdLEASTN  PRIdFASTN  PRIdMAX  PRIdPTR   /* the same for the other families */

SCNd8 SCNd16 SCNd32 SCNd64   SCNi... SCNu... SCNo... SCNx...   /* for scanf */

intmax_t  strtoimax(const char *restrict nptr, char **restrict endptr, int base);
uintmax_t strtoumax(const char *restrict nptr, char **restrict endptr, int base);
intmax_t  imaxabs(intmax_t j);
imaxdiv_t imaxdiv(intmax_t numer, intmax_t denom);
Name Description
PRIxN macros String literals for printf: put them between two string pieces, as in "%" PRId64 "\n".
SCNxN macros The same for the scanf family. They are separate macros because scanf stores through a pointer, so the length modifier must match the exact type.
nptr, endptr, base As for strtol: input text, where parsing stopped, and the base (0 or 2 to 36).
imaxdiv_t Struct with members quot and rem, both intmax_t.

Returns: strtoimax and strtoumax return the converted value. On overflow they return INTMAX_MAX, INTMAX_MIN or UINTMAX_MAX and set errno to ERANGE. imaxabs returns the absolute value; imaxdiv returns quotient and remainder together.

Errors / undefined behavior: imaxabs(INTMAX_MIN) and imaxdiv with a zero divisor (or INTMAX_MIN / -1) are undefined. Using the wrong macro for a type is undefined, just like using the wrong printf specifier.

Usage notes

  • Concatenate, do not format. The macros are plain string literals, so "%" PRId32 is adjacent string literal concatenation done by the compiler. Flags, width and precision go inside the first piece: "%08" PRIx16 and "%-8" PRIu16 work.
  • Alternatives. If you cannot stand the macros, cast to a standard type: printf("%lld", (long long)x) is always correct for any signed value up to 64 bits, and %zu handles size_t.
  • Small types promote anyway. PRId8 is "d" on common platforms, because int8_t promotes to int when passed to printf. For scanf the macro expands to "hhd", which writes only one byte.
  • No PRIb on the runner. C23 adds PRIb32 and friends for binary output and %b for printf, but the runner's glibc 2.28 does not know them, so print binary with a loop over the bits (see printf).
  • Error handling for strtoimax. As with strtol, set errno = 0 first, then check endptr against the start (nothing was parsed) and errno == ERANGE. See strtol and strtoul.
  • Wide versions. wcstoimax and wcstoumax do the same for wide strings.
  • Cost. The macros are free; the functions are as fast as strtoll.
  • Standard version: C99; the binary PRIb and SCNb macros are C23.