t Format specifiers in c?
I want to print out a variable of type size_t
in C but it appears that size_t
is aliased to different variable types on different architectures. For example, on one machine (64-bit) the following code does not throw any warnings:
size_t size = 1;
printf("the size is %ld", size);
but on my other machine (32-bit) the above code produces the following warning message:
warning: format '%ld' expects type 'long int *', but argument 3 has type 'size_t *'
I suspect this is due to the difference in pointer size, so that on my 64-bit machine size_t
is aliased to a long int
( "%ld"
), whereas on my 32-bit machine size_t
is aliased to another type.
Is there a format specifier specifically for size_t
?
Yes: use the z
length modifier:
size_t size = sizeof(char);
printf("the size is %zdn", size); // decimal size_t
printf("the size is %zxn", 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.
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).
MSDN表示,Visual Studio支持在32位和64位平台上可移植代码的“I”前缀。
size_t size = 10;
printf("size is %Iu", size);
链接地址: http://www.djcxy.com/p/72208.html
上一篇: 跨平台格式字符串,用于字体大小的变量
下一篇: t c中的格式说明符?