C examples for Language Basics:printf
The precision specifier is set after the minimum field width specifier.
It consists of a period followed by an integer.
Its exact meaning depends upon the type of data to which it is applied.
When applying the precision specifier to floating-point data using the %f, %e, or %E specifiers, it determines the number of decimal places displayed.
For example, %10.4f displays a number at least 10 characters wide in total and four of them are decimal places.
When the precision specifier is applied to %g or %G, it specifies the number of significant digits.
When applying to strings, the precision specifier sets the maximum field length.
For example, %5.7s displays a string at least five and not exceeding seven characters long. If the string is longer than the maximum field width, the end characters will be truncated.
When applying to int types, the precision specifier sets the minimum number of digits for each number. Leading zeroes are added to match the required number of digits.
The following program illustrates the precision specifier:
#include <stdio.h> int main(void) { printf("%.4f\n", 123.1234567); printf("%3.8d\n", 1000); printf("%10.15s\n", "This is a simple test."); return 0;// w w w. java 2s .c o m }