size_t Format specifiers in c?



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?

share improve this question
 
 
Your warning message does not match the code. The warning mentions pointers, your code doesn't have any. Did you remove some & somewhere? –  Jens  Apr 17 '12 at 14:38
 
Pointers? No I don't get any warnings about pointers, in fact depending on what machine I run that code on sometimes I get no warnings at all. Try the following test code: #include <stdio.h> int main(){ size_t size = 1; printf("the size is %ld", size); return 0; } –  Ethan Heilman  Apr 17 '12 at 16:39
1 
 
@EthanHeilman He's referring to the fact that your warnings say warning: format '%ld' expects type 'long int *', but argument 3 has type 'size_t *' when it probably should be saying warning: format '%ld' expects type 'long int', but argument 3 has type 'size_t'. Were you maybe using scanf() instead when you got these warnings? –  RastaJedi  Aug 20 at 19:04

3 Answers

up vote 70 down vote accepted

Yes: use the z length modifier:

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

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.

share improve this answer
 
 
whats the difference between zd and zu? I get that zd is decimal, but is it signed, if so how does zd being signed effect things. –  Ethan Heilman  Jan 24 '10 at 3:51
1 
It's the difference between a size_t and an ssize_t; the latter is seldomly used. –  Adam Rosenfield  Jan 24 '10 at 3:53
16 
Right, so in this case, you should be using %zu, because the argument is unsigned. –  caf  Jan 24 '10 at 23:03
 
The other options available are explained in the printf manual page: linux.die.net/man/3/printf –  INS  Jan 7 '14 at 22:00
2 
@detly: No, the z length modifier is not part of C89/C90. If you're aiming for C89-compliant code, the best you can do is cast to unsigned long and use the l length modifier instead, e.g. printf("the size is %lu\n", (unsigned long)size);; supporting both C89 and systems with size_t larger than long is trickier and would require using a number of preprocessor macros. –  Adam Rosenfield  Mar 25 '14 at 6:01

Yes, there is. It is %zu (as specified in ANSI C99).

size_t size = 1;
printf("the size is %zu", size);

Note that size_t is unsigned, thus %ld is double wrong: wrong length modifier and wrong format conversion specifier. In case you wonder, %zd is for ssize_t (which is signed).

share improve this answer
 

MSDN, says that Visual Studio supports the "I" prefix for code portable on 32 and 64 bit platforms.

size_t size = 10;
printf("size is %Iu", size);
share improve this answer
 
1 
it's MS specific, which is not standard conforming, so it's not platform independent –  Lưu Vĩnh Phúc  Jun 24 at 7:24

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?

share improve this question
 
   
Your warning message does not match the code. The warning mentions pointers, your code doesn't have any. Did you remove some & somewhere? –  Jens  Apr 17 '12 at 14:38
   
Pointers? No I don't get any warnings about pointers, in fact depending on what machine I run that code on sometimes I get no warnings at all. Try the following test code: #include <stdio.h> int main(){ size_t size = 1; printf("the size is %ld", size); return 0; } –  Ethan Heilman  Apr 17 '12 at 16:39
1 
   
@EthanHeilman He's referring to the fact that your warnings say warning: format '%ld' expects type 'long int *', but argument 3 has type 'size_t *' when it probably should be saying warning: format '%ld' expects type 'long int', but argument 3 has type 'size_t'. Were you maybe using scanf() instead when you got these warnings? –  RastaJedi  Aug 20 at 19:04

3 Answers

up vote 70 down vote accepted

Yes: use the z length modifier:

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

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.

share improve this answer
 
   
whats the difference between zd and zu? I get that zd is decimal, but is it signed, if so how does zd being signed effect things. –  Ethan Heilman  Jan 24 '10 at 3:51
1 
It's the difference between a size_t and an ssize_t; the latter is seldomly used. –  Adam Rosenfield  Jan 24 '10 at 3:53
16 
Right, so in this case, you should be using %zu, because the argument is unsigned. –  caf  Jan 24 '10 at 23:03
   
The other options available are explained in the printf manual page: linux.die.net/man/3/printf –  INS  Jan 7 '14 at 22:00
2 
@detly: No, the z length modifier is not part of C89/C90. If you're aiming for C89-compliant code, the best you can do is cast to unsigned long and use the l length modifier instead, e.g. printf("the size is %lu\n", (unsigned long)size);; supporting both C89 and systems with size_t larger than long is trickier and would require using a number of preprocessor macros. –  Adam Rosenfield  Mar 25 '14 at 6:01

Yes, there is. It is %zu (as specified in ANSI C99).

size_t size = 1;
printf("the size is %zu", size);

Note that size_t is unsigned, thus %ld is double wrong: wrong length modifier and wrong format conversion specifier. In case you wonder, %zd is for ssize_t (which is signed).

share improve this answer
 

MSDN, says that Visual Studio supports the "I" prefix for code portable on 32 and 64 bit platforms.

size_t size = 10;
printf("size is %Iu", size);
share improve this answer
 
1 
it's MS specific, which is not standard conforming, so it's not platform independent –  Lưu Vĩnh Phúc  Jun 24 at 7:24
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值