Hi
i have written a program to check the precision of float and double values.
i read that the precision of float is 6 digits and double is 10.
but it is showing only 6 for both.

do we need to make any arrangement for the system to print the correct precission.

[Shark@localhost Fresh]$ gcc flt_dbl_prec.c
[Shark@localhost Fresh]$ ./a.out
34.345676 
34.345678
[Shark@localhost Fresh]$ cat flt_dbl_prec.c
int  main()
{
        float f = 34.34567832;
        double d= 34.3456783221;
        printf("%f\n",f);
        printf("%lf\n",d);
return 0;
}

>i read that the precision of float is 6 digits and double is 10.
No, the precision of float is FLT_DIG and double is DBL_DIG, where both of those macros are defined in <float.h>.

>but it is showing only 6 for both.
Yes, that's the correct behavior. If you print a floating-point value using printf and no precision specification, the precision is assumed to be 6. You can find this as a stated requirement in the standard for the %f specifier.

>float f = 34.34567832;
You'll most likely get a warning about precision loss here. float constants are suffixed with f or cast to float:

float f = 34.34567832f;

>printf("%lf\n",d);
There's no such thing as thing as %lf. You've invoked undefined behavior by using a length modifier that doesn't apply to the type specifier. %f is for double and %Lf is for long double. Because printf is a variable argument function, all of the variable arguments follow the regular type promotions, which means that float gets promoted to double. Therefore there's no specifier for float in printf.

Try this instead:

#include <stdio.h>
#include <float.h>

int main ( void )
{
  printf ( "Float precision:  %d\n", FLT_DIG );
  printf ( "double precision: %d\n", DBL_DIG );

  printf ( "float value:  %.*f\n", FLT_DIG, 34.34567832f );
  printf ( "double value: %.*f\n", DBL_DIG, 34.3456783221 );

  return 0;
}

>printf("%lf\n",d);
There's no such thing as thing as %lf. You've invoked undefined behavior by using a length modifier that doesn't apply to the type specifier.

but many books have this specifier.
where can i find the information related to the format specifiers.

>but many books have this specifier.
I hate to break it to you, but many books are written by people who know little more about C than you do. The lesson about %lf is that printf is not a mirror image of scanf in terms of format specifiers.

>where can i find the information related to the format specifiers.
A good book will cover them accurately. A good reference will cover them. And of course, you can refer to the standard itself, though it's a bit harder to glean useful information until you learn how to decipher the legalese.

commented: thanks ,its very helpfull +1

>but many books have this specifier.
I hate to break it to you, but many books are written by people who know little more about C than you do. The lesson about %lf is that printf is not a mirror image of scanf in terms of format specifiers.

>where can i find the information related to the format specifiers.
A good book will cover them accurately. A good reference will cover them. And of course, you can refer to the standard itself, though it's a bit harder to glean useful information until you learn how to decipher the legalese.

thnaks Narue,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.