使用 printf 打印 size_t 的正确方法是什么? [英] What's the correct way to use printf to print a size_t?

查看:59
本文介绍了使用 printf 打印 size_t 的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Size_t 被定义为一个 unsigned 整数,但它的大小取决于您使用的是 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?

推荐答案

尝试使用 %zu 格式字符串

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

这篇关于使用 printf 打印 size_t 的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆