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);
but on my other machine (32-bit) the above code produces the following warning message:
warning: format '%ld' expects type 'long int *', but argument 3 has type '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.
Is there a format specifier specifically for size_t?
本文探讨了在C语言中如何正确打印size_t类型的变量。由于size_t在不同架构下可能被别名为不同的类型,因此使用通用的打印格式可能会导致警告。文章提供了平台无关的解决方案,介绍了使用%zd和%zu格式化字符串来正确输出size_t变量。


615







&somewhere? – Jens Apr 17 '12 at 14:38warning: format '%ld' expects type 'long int *', but argument 3 has type 'size_t *'when it probably should be sayingwarning: format '%ld' expects type 'long int', but argument 3 has type 'size_t'. Were you maybe usingscanf()instead when you got these warnings? – RastaJedi Aug 20 at 19:04