Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf("%f",x) ok, printf("%F",x) error too many arguments for format

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]
like image 461
Federica Guidotti Avatar asked Jun 07 '20 09:06

Federica Guidotti


People also ask

What does %s mean in fprintf?

Below are several examples of printing information from variables using fprintf. Notice the use of %s to print a string, and %d to print an integer, and %f to print a number with a decimal (a floating point number).

What is %B in printf?

The Printf module API details the type conversion flags, among them: %B: convert a boolean argument to the string true or false %b: convert a boolean argument (deprecated; do not use in new programs).

What is %A in printf?

The %a formatting specifier is new in C99. It prints the floating-point number in hexadecimal form. This is not something you would use to present numbers to users, but it's very handy for under-the-hood/technical use cases. As an example, this code: printf("pi=%a\n", 3.14); prints: pi=0x1.91eb86p+1.


2 Answers

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.

like image 168
anastaciu Avatar answered Sep 19 '22 23:09

anastaciu


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.

like image 21
RobertS supports Monica Cellio Avatar answered Sep 18 '22 23:09

RobertS supports Monica Cellio