by Dinesh Thakur Category: Type And Variables

Precision is specified by the number of digits after the decimal point for the outputs for float as well as double numbers. If precision is not specified, it would be according to the default setting in the computer which is generally 6 digits. The precision may be specified by a period(.) followed by a positive number equal to the number of digits desired. An illustration is given below.

Float x;

printf("%.6f", x);

The above will display 6 digits after the decimal point. Here, the field width is not specified. Consider the following example where both are specified:

printf("%6.2f", x);

The above code will display the number x in width 6 with two digits after the decimal point. Both are placed between the % symbol and the conversion character. It is also illustrated in the following program. The bars have been included with the purpose of showing the placement of output between the specified width. The space between bars represents the width setting. The following program illustrates the application of precision setting.

Illustrates width and precision setting for a decimal point number.

#include <stdio.h>

main()

{

  double Pi = 3.141592653589793238;

  printf("|%-lf|\n", Pi);

  printf("|%lf|\n", Pi);  // default width setting in computer

  printf("|%30lf|\n", Pi); // width 30 right justification

  printf("|l%-30lf|\n", Pi); //width 30 left justification

  printf("|%030.10f|\n", Pi);

/*width 30 precision 10 right justification. Filling blank space with '0' */

  printf("|%-6.3f|\n", Pi);

  printf("|%-0.15f|\n", Pi);

  printf("|%-030.15f|\n", Pi);

}

The expected output is as given below

    Precision Setting in C

The first two lines of the output are due to default setting in the computer. The width is as much as the number of digits, therefore, right and left justification are same. The third and forth lines of the output are due to specified width of 30, but no precision is specified. So, we get 6 digits after the decimal point (default setting in computer) with right and left justification, respectively. The fifth line of the output is due to the following code:

printf("|%030.10f|\n", Pi);

which specifies a total width of 30 with 10 digits after the decimal, right justification and the empty spaces are to be filled by '0'. For the sixth line of the output, the specifications are (i) width 6, (ii) precision 3, and (iii) left justification. In the output there is only one empty space. For the last but one line of the output, the specifications are (i) no width specified and (ii) precision 15. The width is by default. So, there are no empty spaces. For such a case, the left and right specification is immaterial. The last line of the output corresponds to (i) width= 30, (ii) precision= 15 and left justification and (iii) character '0' to fill empty spaces. But since 0s are added only with right justification, there are no 0s in the output.



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content