Platform independent size_t Format specifiers in c?
Yes: use the z
length modifier:
size_t size = sizeof(char);
printf("the size is %zu\n", size); // decimal size_t ("u" for unsigned)
printf("the size is %zx\n", size); // hex size_t
The other length modifiers that are available are hh
(for char
), h
(for short
), l
(for long
), ll
(for long long
), j
(for intmax_t
), t
(for ptrdiff_t
), and L
(for long double
). See §7.19.6.1 (7) of the C99 standard.
MSDN, says that Visual Studio supports the "I" prefix for code portable on 32 and 64 bit platforms.
size_t size = 10;
printf("size is %Iu", size);
Yes, there is. It is %zu
(as specified in ANSI C99).
size_t size = 1;
printf("the size is %zu", size);
Note that size_t
is unsigned, thus %ld
is double wrong: wrong length modifier and wrong format conversion specifier. In case you wonder, %zd
is for ssize_t
(which is signed).