Looking for clarification on using "zd"
with printf()
.
Certainly the following is correct with C99 and later.
void print_size(size_t sz) {
printf("%zu\n", sz);
}
The C spec seems to allow printf("%zd\n", sz)
depending on how it is read:
7.21.6.1 The fprintf
function
z
Specifies that a followingd
,i
,o
,u
,x
, orX
conversion specifier applies to asize_t
or the corresponding signed integer type argument; or that a followingn
conversion specifier applies to a pointer to a signed integer type corresponding tosize_t
argument. C11dr §7.21.6.1 7
Should this be read as
- "
z
Specifies that a followingd
... conversion specifier applies to asize_t
or the corresponding signed integer type argument ... "(both types) and "z
Specifies that a followingu
... conversion specifier applies to asize_t
or the corresponding signed integer type argument ..." (both types)
OR
- "
z
Specifies that a followingd
... conversion specifier applies to a corresponding signed integer type argument ..." (signed type only) and "z
Specifies that a followingu
... conversion specifier applies to asize_t
" (unsigned type only).
I've been using the #2 definition, but now not so sure.
Which is correct, 1, 2, or something else?
If #2 is correct, what is an example of a type that can use
"%zd"
?