本文介绍了平台在C独立为size_t格式说明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打印出的C类型为size_t 的变量,但现在看来,为size_t 化名为不同变量类型在不同的架构。例如,一台机器上(64位)以下的code不抛出任何警告:

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);

但我的其他机器(32位)以上code产生以下警告消息:

but on my other machine (32-bit) the above code produces the following warning message:

警告:格式'%ld的'需要键入
  长整型*',但参数3的类型
  为size_t *

我怀疑这是由于指针大小的差异,让我的64位计算机上为size_t 的别名到长整型%LD),而我的32位计算机上为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.

有一个格式说明专门为为size_t

Is there a format specifier specifically for size_t?

推荐答案

是:使用以Z 长度修改:

size_t size = sizeof(char);
printf("the size is %zd\n", size);  // decimal size_t
printf("the size is %zx\n", size);  // hex size_t

另外长度修改可用的 HH (为字符), ^ h (为),(为), LL (为长长),Ĵ(为还会将intmax_t ), T ptrdiff_t的)和(为长双)。见§7.19.6.1(7)C99标准。

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.

这篇关于平台在C独立为size_t格式说明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 09:23