printf(“%f”,x)正常,printf(“%F”,x)错误太多格式参数 [英] printf("%f",x) ok, printf("%F",x) error too many arguments for format
问题描述
当我在CodeBlocks中使用说明符F时,为什么编译器给我错误格式的参数过多?
#include< stdio.h>
int main()
{
float x = 3.14159;
printf(%f\n,x);
printf(%F\n,x);
返回0;
}
错误:
<
错误:格式为[-Werror = format =]
的未知转换类型字符'F'错误:的参数过多格式[-Werror = format-extra-args]
看起来有些版本的GCC无法识别%F
,这很奇怪。我的 gcc版本9.2.0(tdm64-1)
对于具有C11标准的Windows,虽然只能发出警告消息而不是错误消息,但无法识别它。
$ gcc main2.c -Wextra -Wall -pedantic -std = c11
main2.c:在函数'main':
main2.c:7:14:警告:未知转换类型字符'F',格式为[-Wformat =]
7 | printf(%F\n,x);
| ^
main2.c:7:12:警告:格式[-Wformat-extra-args]的参数过多
7 | printf(%F\n,x);
| ^ ~~~~~
执行时不打印该值。
我猜您可能在Windows系统中使用了mingW安装,并且您的编译器必须将警告视为错误,这不是一个坏主意。
作为建议使用@HolyBlackCat ,添加 -D__USE_MINGW_ANSI_STDIO
标志即可解决此问题。
此线程包含有关操作方法的说明。
@RobertS支持Monica Cellio答案包含说明链接有关如何将其添加到CodeBlocks中的信息。
或者,要快速修复代码,可以使用%G
,或%E
获取科学计数法。
Why the compiler gives me the error "too many arguments for format" when I use the specifier F in CodeBlocks?
#include <stdio.h>
int main()
{
float x = 3.14159;
printf("%f\n", x);
printf("%F\n", x);
return 0;
}
The errors:
error: unknown conversion type character 'F' in format [-Werror=format=]
error: too many arguments for format [-Werror=format-extra-args]
Looks like some versions of GCC don't recognize %F
, oddly enough. My gcc version 9.2.0 (tdm64-1)
for windows with C11 standard, does not recognize it though it only issues those as warning messages not errors.
$ gcc main2.c -Wextra -Wall -pedantic -std=c11
main2.c: In function 'main':
main2.c:7:14: warning: unknown conversion type character 'F' in format [-Wformat=]
7 | printf("%F\n", x);
| ^
main2.c:7:12: warning: too many arguments for format [-Wformat-extra-args]
7 | printf("%F\n", x);
| ^~~~~~
Upon execution the value is not printed.
I'm guessing you might be using some mingW installation in a Windows system and your compiler must be treating warnings as errors, which is not a bad idea.
As @HolyBlackCat suggested, adding -D__USE_MINGW_ANSI_STDIO
flag solves the issue.
This thread has the instructions on how to do it.
@RobertS supports Monica Cellio answer has a link with instructions on how to add it to CodeBlocks.
Alternatively, for a quick fix of the code you can use %G
, or %E
for scientific notation.
这篇关于printf(“%f”,x)正常,printf(“%F”,x)错误太多格式参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!