sizeof
returns size_t
you need to use %zu
for the format string instead of %d
. The type of unsigned integer of size_t
can vary (depending on platform) and may not be long unsigned int everywhere, which is covered in the draft C99 standard section 6.5.3.4
The sizeof operator paragraph 4:
The value of the result is implementation-defined, and its type (an unsigned integer type)
is size_t, defined in (and other headers).
Also note that using the wrong format specifier for printf
is undefined behavior, which is covered in section 7.19.6.1
The fprintf function, which also covers printf
with respect to format specifiers says:
If a conversion specification is invalid, the behavior is undefined.248) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
Update
Visual Studio
does not support the z
format specifier:
The hh, j, z, and t length prefixes are not supported.
the correct format specifier in this case would be %Iu
.