Is using %zu correct syntax in a printf format string as shown in some C code found on Wikipedia?
Asked Answered
L

6

7

I just found this code on Wikipedia.
Link: http://en.wikipedia.org/wiki/Sizeof#Use

The code:

/* the following code illustrates the use of sizeof 
 * with variables and expressions (no parentheses needed),
 * and with type names (parentheses needed)
 */

char c;

printf("%zu,%zu", sizeof c, sizeof(int));

It states that: "The z prefix should be used to print it, because the actual size can differ on each architecture."

I tried it on my compiler, but it gives the following result:

zu,zu
Leia answered 28/5, 2010 at 16:18 Comment(0)
M
7

Yes that syntax is correct (at least for C99). Looks like your compiler isn't set up to handle it though. Just take out the z and you'll probably be fine. To be correct, make sure your printf format specifiers match the size of the types. Turning on all the warnings your compiler will give you probably helps out in that respect.

Your quotation:

The z prefix should be used to print it, because the actual size can differ on each architecture

is referring to the fact that size_t (which is the type returned by the sizeof operator) can vary from architecture to architecture. The z is intended to make your code more portable. However, if your compiler doesn't support it, that's not going to work out. Just fiddle with combinations of %u, %lu, etc. until you get the output making sense.

Miasma answered 28/5, 2010 at 16:19 Comment(1)
You can use %lu and explicitly cast the sizeof value to unsigned long - as long as you know that the size of the object is less than 4GB you know then that it will definitely be reported correctly.Shahjahanpur
C
6

The z length modifier was added to C in the C99 standard; you might have a compiler that doesn't support C99.

If your C compiler doesn't support that, you can probably treat the sizes as unsigned long:

printf("%lu,%lu", (unsigned long)sizeof c, (unsigned long)sizeof(int));
Clothes answered 28/5, 2010 at 16:21 Comment(0)
P
2

Yes, but it only works on C99-compliant compilers. From wikipedia:

z: For integer types, causes printf to expect a size_t sized integer argument.

Perloff answered 28/5, 2010 at 16:23 Comment(0)
I
1

Did you tell your compiler that you want it thinking with a C99 brain? There is probably a switch to do that. For instance, -std=c99 for gcc.

If your compiler does not support it, but you know others will, you can do a PRId64 style work around (disclaimer - PSEUDO CODE AHEAD ..):

#ifdef __SOME_KNOWN_C99_COMPILER
#define PORTUNSIGNED "zu"
#else
#define PORTUNSIGNED "u"
#endif

printf("%-11" PORTUNSIGNED " ways to skin a cat\n");

Its probably better to get a compiler that has functional support for c99, however.

Irvine answered 28/5, 2010 at 16:22 Comment(2)
The preprocessor is not evil.Auster
@George - I'm not saying its evil, I'm just saying that some stop actually using it and start improvising with it, which leads to interesting things that other people eventually have to maintain :)Irvine
D
0

I've made a test using gcc 4.0. It works with -std=c99

Delphine answered 28/5, 2010 at 17:34 Comment(0)
F
0

Thanks for the -std=c99 suggestion @Tim Post and @pcent. For novices like me, a bit more detail: I'm using Windows and ming_w32_gcc. When I compiled my code using:

gcc -Werror -Wall q3.c

I got the error message "unknown conversion type character 'z' in format" But when I added -std=c99 on the end it worked fine:

gcc -Werror -Wall q3.c -std=c99
Footworn answered 7/8, 2023 at 23:51 Comment(1)
This just affirms an existing answer. Please upvote instead of posting 'thank you' answers.Sulcus

© 2022 - 2024 — McMap. All rights reserved.