|
Precision Specifiers
The
Precision Specifier
This specifier follows the minimum field
width specifier, if it is present. It consists of a pertod (.) followed by
an integer. Its exact meaning depends on the type of data it is applied
to. When applied to fioatingpoint data, this specifier determines the
number of decimal places displayed. For example, %12.5f displays a number
of at least 12 characters width having 5 decimal places.
With strings, the precision specifier specifies the maximum field length.
For example, %8.lOs displays a string of minimum 8 characters, at the same
time not exceeding 10 characters. If the strtng is longer than the
specified maximum, then the end characters are truncated.
When applied to integer types, the precision specifier deterrnines the
minimum number of digits that will appear for each number. Leading zeros
are added to achieve the required number of digits.
#include
<stdio.h>
main ( )
{
float num1;
int num2;
char str1[80]= "An";
char str2[80]= "This will test precision";
num1 = 13.4485;
num2 = 5887;
printf("The float is %.3f\n", num1);
printf("The integer is %.7d\n", num2)
printf("%5.20s\n", str1);
printf("%5.20s\n", str2);
getchar();
}
Output :
The program
will produce the following result :
The float is 13.448 The integer is 0005887
|