Using precision while printing integers, floating-point numbers, and strings : printf Precision « printf scanf « C Tutorial






#include <stdio.h>

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;

}
Using precision for integers
        0873
        000000873

Using precision for floating-point numbers
        123.945
        1.239e+02
        124

Using precision for strings
        Happy Birth








4.9.printf Precision
4.9.1.For floating arguments, precision indicates how many digits are printed after decimal points.
4.9.2.Precision
4.9.3.Using precision while printing integers, floating-point numbers, and strings