printf()一个size_t实例的便携方式 [英] Portable way to printf() a size_t instance

查看:90
本文介绍了printf()一个size_t实例的便携方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



您好,


我的代码可以使用size_t类型的变量。代码是

最初是在32位机器上开发的,但现在它在32位机器和64位机器上运行。

在代码中有这样的语句:


size_t buffer_size;

printf("总缓冲区大小:%ud bytes \ n" ;,buffer_size);


现在格式为%ud在32位计算机上运行良好;它实际上

也适用于64位计算机,但编译器吐出

警告消息。在64位计算机上,它首选:


printf(总缓冲区大小:%uld bytes \ n,buffer_size); / *或者udl? * /


正如我所说它有效,但我真的更喜欢这个程序在没有警告的情况下编译

,因为现在我得到*很多*警告类型


file.c:line:warning:unsigned int format,不同类型arg(arg 1)。


然后,打开有几次,这实际上让我错过了更多

重要警告。

任何提示赞赏。

Joakim

-

Joakim Hove

hove AT ift uib no /

Tlf:+47(55 5)8 27 90 / Stabburveien 18

传真:+47(55 5)8 94 40 / N-5231 Paradis
http://www.ift.uib.no/~hove/ / 55 91 28 18/92 68 57 04


Hello,

I have code which makses use of variables of type size_t. The code is
originally developed on a 32 bit machine, but now it is run on both a
32 bit and a 64 bit machine.

In the code have statements like this:

size_t buffer_size;
printf("Total buffer size: %ud bytes \n",buffer_size);

Now the format "%ud" works nicely on the 32 bit computer; it actually
works on the 64 bit computer as well, but the compiler spits out
warning message. On the 64 bit computer it would have prefered:

printf("Total buffer size: %uld bytes \n",buffer_size); /* Or udl? */

As I said it works, but I would really prefer the program to compile
without warnings, as it is now I get *many* warnings of the type

file.c:line: warning: unsigned int format, different type arg (arg 1).

And, on several occasion this has actually led me to miss more
important warnings.
Any tips appreciated.
Joakim
--
Joakim Hove
hove AT ift uib no /
Tlf: +47 (55 5)8 27 90 / Stabburveien 18
Fax: +47 (55 5)8 94 40 / N-5231 Paradis
http://www.ift.uib.no/~hove/ / 55 91 28 18 / 92 68 57 04

推荐答案

Joakim Hove写道:
Joakim Hove wrote:

你好,

我有代码可以使用size_t类型的变量。代码最初是在32位机器上开发的,但现在它可以在32位和64位机器上运行。

在代码中有这样的语句:

size_t buffer_size;
printf("总缓冲区大小:%ud bytes \ n",buffer_size);

现在格式为%ud ;在32位计算机上运行良好;它实际上也适用于64位计算机,但编译器会发出警告消息。在64位计算机上,它首选:

printf(总缓冲区大小:%uld bytes \ n,buffer_size); / *或者udl? * /

正如我所说它有效,但我真的更喜欢这个程序在没有警告的情况下编译
,因为现在我得到了*很多*类型的警告

file.c:line:warning:unsigned int format,不同类型的arg(arg 1)。

而且,有几次这实际上让我错过了更多
重要的警告。

任何提示赞赏。

Hello,

I have code which makses use of variables of type size_t. The code is
originally developed on a 32 bit machine, but now it is run on both a
32 bit and a 64 bit machine.

In the code have statements like this:

size_t buffer_size;
printf("Total buffer size: %ud bytes \n",buffer_size);

Now the format "%ud" works nicely on the 32 bit computer; it actually
works on the 64 bit computer as well, but the compiler spits out
warning message. On the 64 bit computer it would have prefered:

printf("Total buffer size: %uld bytes \n",buffer_size); /* Or udl? */

As I said it works, but I would really prefer the program to compile
without warnings, as it is now I get *many* warnings of the type

file.c:line: warning: unsigned int format, different type arg (arg 1).

And, on several occasion this has actually led me to miss more
important warnings.

Any tips appreciated.




对于C89,转换为long unsigned。


printf (" sizeof(int)是%lu \ n",(long unsigned)sizeof(int));


-

pete



For C89, convert to long unsigned.

printf("sizeof(int) is %lu\n", (long unsigned)sizeof(int));

--
pete


Joakim Hove写道:
Joakim Hove wrote:
你好,

我有代码可以使用size_t类型的变量。代码最初是在32位机器上开发的,但现在它可以在32位和64位机器上运行。

在代码中有这样的语句:

size_t buffer_size;
printf("总缓冲区大小:%ud bytes \ n",buffer_size);

现在格式为%ud ;在32位计算机上运行良好;


不应该。 %< qualifier> d是签名

整数的转换说明符; %< qualifier> u是unsigned

整数的转换说明符。 ''你'不是%d的限定符。


它实际上也适用于64位计算机,但编译器吐出
警告信息。


类似%ud并不代表任何东西?

在64位计算机上它会更喜欢:

printf(" Total buffer size:%uld bytes \ n",buffer_size); / *或者udl? * /
Hello,

I have code which makses use of variables of type size_t. The code is
originally developed on a 32 bit machine, but now it is run on both a
32 bit and a 64 bit machine.

In the code have statements like this:

size_t buffer_size;
printf("Total buffer size: %ud bytes \n",buffer_size);

Now the format "%ud" works nicely on the 32 bit computer;
It shouldn''t. The %<qualifier>d are the conversion specifiers for signed
integers; the %<qualifier>u are the conversion specifiers for unsigned
integers. ''u'' is not a qualifier for %d.

it actually
works on the 64 bit computer as well, but the compiler spits out
warning message.
Something like "%ud doesn''t mean anything"?
On the 64 bit computer it would have prefered:

printf("Total buffer size: %uld bytes \n",buffer_size); /* Or udl? */




我对此表示怀疑。 printf例程很少想要没有意义的说明符。

如果你有一个最新的库,你可以使用

printf(总缓冲区大小:%zu bytes \ n,buffer_size );


否则,请尝试

printf(总缓冲区大小:%lu bytes \ n,

( long unsigned)buffer_size);



printf("总缓冲区大小:%llu bytes \ n",

(long long) unsigned)buffer_size);


也就是说,使用(正确编写的)无符号说明符并将

size_t参数转换为相同大小的unsigned。



I doubt it. The printf routine rarely "wants" meaningless specifiers.
If you have an up-to-date library, you could use
printf("Total buffer size: %zu bytes \n",buffer_size);

Otherwise, try
printf("Total buffer size: %lu bytes \n",
(long unsigned)buffer_size);
or
printf("Total buffer size: %llu bytes \n",
(long long unsigned) buffer_size);

That is, use an (properly written) unsigned specifier and cast the
size_t argument to the same size unsigned.


Martin Ambuhl写道(现在澄清):
Martin Ambuhl wrote (and now clarifies):
Joakim Hove写道:
[...]
Joakim Hove wrote: [...]
现在格式为%ud在32位计算机上运行良好;
Now the format "%ud" works nicely on the 32 bit computer;



它不应该。 %< qualifier> d是签名
整数的转换说明符; %< qualifier> u是无符号
整数的转换说明符。 ''你'不是%d的限定符。


It shouldn''t. The %<qualifier>d are the conversion specifiers for signed
integers; the %<qualifier>u are the conversion specifiers for unsigned
integers. ''u'' is not a qualifier for %d.




我应该更清楚。 "%U"是您正在使用的说明符。

" d"是输出字符串的文字部分。请参阅下面的示例

并注意字符串末尾的额外d:


#include< stdio.h>

int main(无效)

{

printf("使用%% ud打印无符号值:%ud \ n,5u) ;

返回0;

}


使用%ud打印无符号值:5d



I should have been clearer. "%u" is the specifier you are using. The
"d" is a literal portion of the output string. See the example below
and note the extra ''d'' at the end of the string:

#include <stdio.h>
int main(void)
{
printf("Printing an unsigned value using %%ud: %ud\n", 5u);
return 0;
}

Printing an unsigned value using %ud: 5d


这篇关于printf()一个size_t实例的便携方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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