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

前端 未结 12 1128
时光说笑
时光说笑 2020-11-22 05:40

I have a variable of type size_t, and I want to print it using printf(). What format specifier do I use to print it portably?

In 32-bit ma

12条回答
  •  半阙折子戏
    2020-11-22 05:58

    Use the z modifier:

    size_t x = ...;
    ssize_t y = ...;
    printf("%zu\n", x);  // prints as unsigned decimal
    printf("%zx\n", x);  // prints as hex
    printf("%zd\n", y);  // prints as signed decimal
    

提交回复
热议问题