[英]C printf specifier %lld prints characters “ld”
I am developing using gcc (-std=gnu99) for an embedded toolchain (Myriota), and I have problems with printf.我正在使用 gcc (-std=gnu99) 进行嵌入式工具链 (Myriota) 的开发,但 printf 存在问题。
when I try the following code:当我尝试以下代码时:
long long int time = TimeGet();
printf("\nSeconds since epoch: %lld\r\n", time);
it prints:它打印:
Seconds since epoch: ld
Using "%" PRId64
prints the same "ld".使用
"%" PRId64
打印相同的“ld”。
Any ideas?有任何想法吗? I'd appreciate if you could point me to the right place.
如果您能指出我正确的地方,我将不胜感激。
Edit variable type corrected long long int time
编辑变量类型更正
long long int time
Most likely, your C library, specifically its implementation of printf
doesn't support C99.最有可能的是,您的 C 库,特别是
printf
的实现不支持 C99。
The type long long int
and the %lld
format were introduced by the 1999 ISO C standard (C99). long long int
类型和%lld
格式是由 1999 ISO C 标准 (C99) 引入的。 Using gcc -std=c99
makes the compiler attempt to conform to C99, but it can't make the runtime library do things that it doesn't implement.使用
gcc -std=c99
使编译器尝试符合 C99,但它不能使运行时库执行它未实现的事情。 You have a mismatch between what the compiler supports and what the runtime library supports.编译器支持的内容与运行时库支持的内容不匹配。
In C90, calling printf
with %lld
in the format string had undefined behavior.在 C90 中,使用格式字符串中的
%lld
调用printf
具有未定义的行为。
Does %ld
work for an argument of type long int
? %ld
是否适用于long int
类型的参数? If the argument doesn't exceed LONG_MAX
, converting and using %ld
might be a good workaround.如果参数不超过
LONG_MAX
,则转换和使用%ld
可能是一个很好的解决方法。 If you need to print values that exceed LONG_MAX
, implementing a long long int
to string conversion isn't horribly difficult.如果您需要打印超过
LONG_MAX
的值,实现long long int
到字符串的转换并不是非常困难。
The Myriota SDK uses newlib-nano which does not support int64_t. Myriota SDK 使用不支持 int64_t 的 newlib-nano。 I'll have to implement my own function to convert to char string, or cast to uint32_t.
我必须实现我自己的 function 以转换为 char 字符串,或转换为 uint32_t。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.