什么是用printf打印为size_t的正确方法是什么?

什么是用printf打印为size_t的正确方法是什么?

问题描述:

为size_t被定义为一个无符号整数,但它的大小取决于你是否是一个32位或64位计算机上。什么是打印出为size_t正确的和可移植的方法?

Size_t is defined as an unsigned integer, but the size of it depends on whether you're on a 32 or 64-bit machine. What's the correct and portable way to print out a size_t?

尝试使用%祖格式字符串

Try using the %zu format string

size_t val = get_the_value();
printf("%zu",val);

在Z部分长度说明它说的论点将在长度size_t型。

The z portion is a length specifier which says the argument will be size_t in length.

源代码 - http://en.wikipedia.org/wiki/Printf#printf_format_placeholders