简体   繁体   中英

Is the %zu specifier required for printf?

We are using C89 on an embedded platform. I attempted to print out a size_t , but it did not work:

#include <stdio.h>
int main(void) {
    size_t n = 123;
    printf("%zu\n",n);
    return 0;
}

Instead of 123 , I got zu .
Other specifiers work correctly.

If size_t exists shouldn't zu also be available in printf ?
Is this something I should contact my library vendor about, or is a library implementation allowed to exclude it?

If size_t exists shouldn't zu also be available in printf?

size_t existed at least since C89 but the respective format specifier %zu (specifically the length modifier z ) was added to the standard only since C99.

So, if you can't use C99 (or C11) and had to print size_t in C89, you just have to fallback to other existing types, such as:

printf("%lu\n", (unsigned long)n);

size_t is returned by sizeof(). %zu specifier prints the length.So to return the size of n,the code should read: #include <stdio.h>

int main(void) { size_t n=123; printf("%zu\\n",sizeof (n)); return 0;` }

This is working fine. In my system, it's printing the expected value. You may be getting the error due to the compiler. I am using the GCC compiler. If you have this only and trying to get the output the first update the GCC and then try. It will work.

你的程序输入

程序的输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM