Search code examples
cprintfdoubleprecision

Adding Double Variables in C doesn't gives decimals on print


I am a newbie in c.
If I try to print the sum of two double variables with 4.0 and 10.0, like:

printf("%lf", b+d); //b = 4.0 , d = 10.0

, the print isn't 14.0, it's 14 without decimals.

What should I do now?


Solution

  • You can write

    printf( "%.1f", b + d );
    

    the precision specifier (in the example above its value is 1) after the point specifies the number of digits to appear after the decimal-point.