printf("%f",x) ok, printf("%F",x) error too many arguments for format
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.
The F
format specifier was first introduced in C99. Your compiler either seems to be compliant to C89/C90 or the std=c90
/std=c89
compiler option is enabled.
If you configured compiler is gcc, you can use the gcc --version
command to detect the version.
Else you should check the set compiler options for which standard the compiler uses. Take a look at here:
How to add compiler flags on codeblocks
Although for Ubuntu (I don´t know on what OS you are using CodeBlocks), but this answer gives you an visual overview of the set up for compiler options in CodeBlocks.
The compiler gives you the error "too many parameters for printf" because it doesn't recognize %F
as a format specifier.... so the parameter you have added to printf()
is extra, and should not be there.
The standard format specifiers from C89 below, specify that the floating point formats are e
, E
, f
, g
and G
(does not include F
, the reason is stated in the last edit of this answer)
Remember that the compiler shouldn't read the format string of printf()
at all to match the parameters with the format specifiers, so what is happening there should only deal with the printf(3)
specification, and that is indeed an issue for printf(3)
not for the compiler. Probably if you try the generated program it should work.
EDIT
I have tried on clang (sorry, but I have no gcc here) and I have discovered some possible cause of the issue (not an error, either). The printf(3)
implementation here, does never switch to scientific notation at all (which is something I have not checked with the standard) so it is never going to generate an alphabetic character and no lowercase or uppercase letter is concerned. So for the program
#include <stdio.h>
int main()
{
printf("%5.3G\n", 3.141592654E160);
}
it prints:
$ ./a.out
3.14E+160
$ _
while for
#include <stdio.h>
int main()
{
printf("%5.3F\n", 3.141592654E160);
}
it prints
$ a.out
31415926539999999255132844331312943389972993386142531366742209094398699375217155068328829400434148008839629239544769533043070670437328460352417427610347451187200.000
$ _
As only digits and decimal point are emitted, there's no uppercase or lowercase interpretation on the case of the format specifier, making both forms equivalent (but one being nonstandard).
The solution is just to switch to lowercase f
.
As @chux-ReinstateMonica suggests in one of the comments, C89, page 133 (143 of the pdf), the standard doesn't include the F
format specifier, only e
, E
, f
, g
and G
. This should be normal, considering that f
never changes to exponential notation.