C printf precision
Description
For floating arguments, precision indicates how many digits are printed after decimal points.
Example 1
output float point value with four decimal places
#include <stdio.h>
main()/*www . jav a2s .c o m*/
{
printf("%10.4e\n", 35.99999);
}
The code above generates the following result.
Example 2
Precision indicates the minimum number of digits printed for type integers d, i, o, u, x, and X.
In the following code 10
is the field-width and 4
is the precision.
#include <stdio.h>
main()//from www. j a va2 s. co m
{
printf("%10.4d\n", 35);
}
The code above generates the following result.
Example 3
Using precision while printing integers, floating-point numbers, and strings
#include <stdio.h>
/*from w w w. java 2 s . c o m*/
int main()
{
int i = 873;
double f = 123.94536;
char s[] = "Happy Birthday";
printf( "Using precision for integers\n" );
printf( "\t%.4d\n\t%.9d\n\n", i, i );
printf( "Using precision for floating-point numbers\n" );
printf( "\t%.3f\n\t%.3e\n\t%.3g\n\n", f, f, f );
printf( "Using precision for strings\n" );
printf( "\t%.11s\n", s );
return 0;
}
The code above generates the following result.