6

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. 
Scooter
  • 6,802
  • 8
  • 41
  • 64

1 Answers1

7

The printf() function is a variadic, or vararg, function; one that accepts a variable number of arguments. The way the printf() function is defined in cppreference.com is as follows:

int printf( const char *format, ... );

The ... indicates that this function can accept a variable number of arguments.

In a variadic function, if a variable of type float is passed to it, it will automatically convert it to a double value; an implicit conversion from type float to type double is done.

In your code, you pass a floating point-type variable value to printf(), as an additional argument in addition to the "%f\n" parameter. Hence, since printf() is a vararg function, it convert value to type double before passing it as an argument. Depending on how high you have kept your compiler's warnings, it is giving you that warning.

Hope this helps!

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31