%zu格式说明符在C99中不起作用。

huangapple go评论127阅读模式
英文:

%zu format specifier with C99 not working

问题

I'm willing to print a size_t value using the %zu format specifier in my format string, however, I always get "zu" as an output, rather than the actual value in my size_t variable:

size_t val = 10;
printf("val: %zu\n", val);  // outputs "zu", not "10"

I've seen that for other people that faced a similar issue, the fix was to set the C standard to C99 or above.

I'm building my project with CMake, and I have the following line in it:

# Set the C standard to C11
set(CMAKE_C_STANDARD 11)

So, I'd assume that I'm good to go, but no, I'm still getting the same issue.

Could I be missing something?

I'm using the following stack:

  • CMake version 3.22
  • Cross compiling with the arm-none-eabi-gcc version 10.3.1 toolchain
  • compilation flags: -Os -g -ffunction-sections -fdata-sections -fno-common -fmessage-length=0 -mcpu=cortex-m4 -mthumb -mthumb-interwork -mlittle-endian -mfloat-abi=hard -mfpu=fpv4-sp-d16

I've also added the -std=c11 compilation option just in case. Still does not work.

英文:

I'm willing to print a size_t value using the %zu format specifier in my format string, however, I always get "zu" as an output, rather than the actual value in my size_t variable:

size_t val = 10;
printf("val: %zu\n", val);  // outputs "zu", not "10"

I've seen that for other people that faced a similar issue, the fix was to set the C standard to C99 or above.

I'm building my project with CMake, and I have the following line in it:

# Set the C standard to C11
set(CMAKE_C_STANDARD 11)

So, I'd assume that I'm good to go, but no, I'm still getting the same issue.

Could I be missing something?

I'm using the following stack:

  • CMake version 3.22
  • Cross compiling with the arm-none-eabi-gcc version 10.3.1 toolchain
  • compilation flags: -Os -g -ffunction-sections -fdata-sections -fno-common -fmessage-length=0 -mcpu=cortex-m4 -mthumb -mthumb-interwork -mlittle-endian -mfloat-abi=hard -mfpu=fpv4-sp-d16

I've also added the -std=c11 compilation option just in case. Still does not work.

答案1

得分: 2

I can provide a translation of the text you provided:

新的标准C库实现正在使用的是其“nano”版本,它禁用了size_tprintf中的long long。您可以使用以下命令进行测试:

$ printf "%s\n" "#include <stdio.h>" "#include <stdint.h>" 'int main() { printf("%td\n", (ptrdiff_t)1); }' | arm-none-eabi-gcc --specs=nano.specs --specs=rdimon.specs -xc -
$ ./a.out # 使用binfmt或arm-none-eabi-gdb模拟器模式运行

请注意,我只提供了文本的翻译部分,没有包含代码部分。

英文:

> Could I be missing something?

Newlib standard C library implementation you are using disables size_t and also long long in printf in its "nano" version.

You can test with:

$ printf &quot;%s\n&quot; &quot;#include &lt;stdio.h&gt;&quot; &quot;#include &lt;stdint.h&gt;&quot; &#39;int main() { printf(&quot;%td\n&quot;, (ptrdiff_t)1); }&#39; | arm-none-eabi-gcc --specs=nano.specs --specs=rdimon.specs -xc -
$ ./a.out  # run with binfmt or arm-none-eabi-gdb simulator mode
td

答案2

得分: 1

The format length modifier z is not supported by the C library used on your target system. Either it is too old and does not support C99 extensions, or it is purposely crippled to save some (rather small) amount of code and only supports %d, %u, and %x. You should cast size_t values as (unsigned) and use %u:

size_t val = sizeof(int);
printf("val: %u\n", (unsigned)val);  // probably outputs "val: 4"
英文:

The format length modifier z is not supported by the C library used on your target system. Either it is too old and does not support C99 extensions, or it is purposely crippled to save some (rather small) amount of code and only supports %d, %u and %x. You should cast size_t values as (unsigned) and use %u:

size_t val = sizeof(int);
printf(&quot;val: %u\n&quot;, (unsigned)val);  // probably outputs &quot;val: 4&quot;

答案3

得分: 0

标准库(newlib)在工具链(由arm提供的Arm GNU Toolchain 12.3.Rel1)中使用,未启用c99 I/O格式支持。如果未定义_WANT_IO_C99_FORMATS,则无法使用%zu作为格式说明符。您可以使用--enable-newlib-io-c99-formats配置编译您的标准库,使其工作。

英文:

The standard library (newlib) used in the toolchain (Arm GNU Toolchain 12.3.Rel1 provided by arm) does not have the c99 I/O format support enabled. If _WANT_IO_C99_FORMATS is not defined, %zu cannot be used as a format specifier. You can compile your standard library with the --enable-newlib-io-c99-formats configuration to make it work.

huangapple
  • 本文由 发表于 2023年8月4日 23:23:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定