By Ethan Heilman


2010-01-24 03:47:11 8 Comments

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?

3 comments

@Adam Rosenfield 2010-01-24 03:49:24

Yes: use the z length modifier:

size_t size = sizeof(char);
printf("the size is %zu\n", size);  // decimal size_t ("u" for unsigned)
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.

@Ethan Heilman 2010-01-24 03:51:54

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.

@Adam Rosenfield 2010-01-24 03:53:25

It's the difference between a size_t and an ssize_t; the latter is seldomly used.

@caf 2010-01-24 23:03:57

Right, so in this case, you should be using %zu, because the argument is unsigned.

@INS 2014-01-07 22:00:53

The other options available are explained in the printf manual page: linux.die.net/man/3/printf

@detly 2014-03-24 22:53:32

Is this also part of C89/90?

@Adam Rosenfield 2014-03-25 06:01:07

@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.

@Arkantos 2015-10-14 19:03:33

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

@phuclv 2016-06-24 07:24:10

it's MS specific, which is not standard conforming, so it's not platform independent

@maxschlepzig 2014-03-01 13:24:06

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

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] Improve INSERT-per-second performance of SQLite?

8 Answered Questions

[SOLVED] unsigned int vs. size_t

  • 2008-09-25 07:00:03
  • Rob
  • 195016 View
  • 475 Score
  • 8 Answer
  • Tags:   c++ c size-t

5 Answered Questions

[SOLVED] Correct format specifier for double in printf

12 Answered Questions

[SOLVED] What is size_t in C?

  • 2010-03-31 05:51:55
  • Vijay
  • 667479 View
  • 583 Score
  • 12 Answer
  • Tags:   c int size-t

12 Answered Questions

[SOLVED] How can one print a size_t variable portably using the printf family?

  • 2010-03-26 15:52:25
  • Arun
  • 260639 View
  • 361 Score
  • 12 Answer
  • Tags:   c printf

6 Answered Questions

[SOLVED] How to print a int64_t type in C

  • 2012-02-10 09:31:33
  • rtacconi
  • 366204 View
  • 267 Score
  • 6 Answer
  • Tags:   c stdint

1 Answered Questions

[SOLVED] Format specifier for scanning long unsigned int

  • 2012-08-27 18:19:59
  • user1391362
  • 56134 View
  • 11 Score
  • 1 Answer
  • Tags:   c scanf

1 Answered Questions

[SOLVED] size_t format warning with gcc std=gnu90

1 Answered Questions

[SOLVED] How to print size_t type data in c?

  • 2015-08-09 19:07:04
  • Sourav Suman
  • 840 View
  • -2 Score
  • 1 Answer
  • Tags:   c

3 Answered Questions

[SOLVED] variables of type size_t and ptrdiff_t

  • 2011-10-31 16:43:37
  • Rich
  • 4658 View
  • 20 Score
  • 3 Answer
  • Tags:   c++ c size-t

Sponsored Content