Why is clang 3.8 warning about using float with% f printf format specifier?
This code:
#include <stdio.h>
int main(void)
{
float value = 10.10f;
printf("%f\n",value);
}
compiled with the -Weverything option using clang 3.8 on Ubuntu 14.04.1 gives:
clang -Weverything w-double-precision.c
w-double-precision.c:5:17: warning: implicit conversion increases floating-point precision: 'float' to 'double' [-Wdouble-promotion]
printf("%f\n",value);
~~~~~~ ^~~~~
1 warning generated.
The function printf()
is a variadic or vararg function ; one that takes a variable number of arguments. The way the printf () function is defined in cppreference.com is as follows:
int printf( const char *format, ... );
...
indicates that this function can take a variable number of arguments.
In a function variable, if a type variable is passed to it float
, it automatically converts it to a value double
; an implicit conversion from type float
to type is performed double
.
In your code, you are passing a floating point variable value
in printf()
as an additional argument in addition to the parameter "%f\n"
. Consequently, as printf()
is vararg function, it converts value
to print double
before passing it as an argument. Depending on how high you have stored the compiler warnings it gives you this warning.
Hope this helps!