c - mingw 环境中 64 位字符串格式化程序的警告
问题描述
在最新版本的 mingw-w64 上避免 64 位 printf/scanf 参数警告的正确方法是什么?
我知道 mingw-w64 的许多标准库函数依赖于 microsoft 运行时,并且这些函数与 C99 标准不兼容。
旧答案说要使用%I64d,但由于 C99 标准是%lld,无论如何,如果我使用-Wall -pedantic编译,我会收到两种语法的警告,这是一个小例子:
#include <stdio.h>
int main(void)
{
long long test;
scanf("%I64d", &test);
scanf("%lld", &test);
}
用gcc编译(我的版本是mingw-w64 5.0.4,gcc 8.2.0):
x86_64-w64-mingw32-gcc -o test.exe test.c -Wall -pedantic
它给出以下警告:
dev:tmp dev$ x86_64-w64-mingw32-gcc -o test.exe test.c -Wall -pedantic
test.c: In function ‘main’:
test.c:6:11: warning: ISO C does not support the ‘I64’ ms_scanf length modifier [-Wformat=]
scanf("%I64d", &test);
^~~~~~~
test.c:6:11: warning: ISO C does not support the ‘I64’ ms_scanf length modifier [-Wformat=]
test.c:7:14: warning: unknown conversion type character ‘l’ in format [-Wformat=]
scanf("%lld", &test);
^
test.c:7:11: warning: too many arguments for format [-Wformat-extra-args]
scanf("%lld", &test);
^~~~~~
test.c:7:14: warning: unknown conversion type character ‘l’ in format [-Wformat=]
scanf("%lld", &test);
^
test.c:7:11: warning: too many arguments for format [-Wformat-extra-args]
scanf("%lld", &test);
^~~~~~
删除-Wall删除两个警告,没有-pedantic我可以在没有警告的情况下编译第 6 行 (%I64d) 但不能编译第 7 行 (%lld)。
解决方案
如果你想编译到微软平台,你不能拘泥于遵循标准。拥抱,扩展(和熄灭)!删除-pedantic
或获取正确的 C99/C11/C17 实现。
C89 不支持%lld
。-Wformat
如果您碰巧将支持它作为扩展的库作为目标,您似乎需要禁用。
推荐阅读
- ada - 多个链接的 GPRBuild 项目中的相同基础包
- javascript - 在 WebStorm 中配置 Jest
- java - 确保 Kafka 消费者注册到消费者在多台机器上运行的未分配分区
- xslt-1.0 - 如何为格式不正确的 XML 结构添加额外的根节点?
- regex - Perl:word@word 的基本 perl 正则表达式解析
- javascript - 无法读取未定义的属性“用户”(React-Native)
- python - Django FloatField 和 DecimalField 创建返回与数据库中的对象不同
- r - 如何在 R Markdown 中将头部结果居中?
- php - WordPress 自定义页面 CSS
- c# - 从控制器 ASP.Net MVC 5 绑定 jqGrid 中的问题