跨平台格式字符串为size_t类型的变量? [英] Cross platform format string for variables of type size_t?

查看:152
本文介绍了跨平台格式字符串为size_t类型的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个跨平台的C / C ++项目的(Win32,Linux和OSX),我需要使用* printf的函数来打印size_t类型的一些变量。在某些环境中为size_t的是8个字节,别人他们是4.在glibc的我有ZD%,而在Win32我可以使用的%ID 。有没有办法解决这个优雅的方式?

On a cross platform c/c++ project (Win32, Linux, OSX), I need to use the *printf functions to print some variables of type size_t. In some environments size_t's are 8 bytes and on others they are 4. On glibc I have %zd, and on Win32 I can use %Id. Is there an elegant way to handle this?

推荐答案

PRIuPTR 宏(来自以下; inttypes.h>)定义了一个十进制格式 uintptr_t形式,它应该永远是够大,你可以蒙上为size_t 来它不截断,如:

The PRIuPTR macro (from <inttypes.h>) defines a decimal format for uintptr_t, which should always be large enough that you can cast a size_t to it without truncating, e.g.

fprintf(stream, "Your size_t var has value %" PRIuPTR ".", (uintptr_t) your_var);

这篇关于跨平台格式字符串为size_t类型的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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